Guest User

Untitled

a guest
Sep 6th, 2017
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. # Assembly Program to sort array of number using AT&T syntax
  2. # Author : Ayush Mishra, Roll Number : 1501CS16
  3.  
  4. # Data directive : initialize data section
  5. .data
  6. arr : .quad 25,35,99,24,5,1,13,42,34
  7. num : .space 8
  8. msg : .string " "
  9. nline : .string "\n"
  10.  
  11. # Text directive : Executable code Section
  12. .text
  13. .globl _start
  14.  
  15. _start:
  16. movq $9,%rcx #rcx <- 9 contain no. of element in array
  17. movq $0,%rdx #rdx <- 0 contain offset of various element
  18.  
  19. sort : #sort for sorting array
  20. movq arr(,%rdx),%rax #rax <- arr+rdx
  21. decq %rcx
  22. movq %rdx,%rdi
  23. movq %rdx,%rsi
  24. cmpq $0,%rcx #checking whether we reach end of array
  25. je printloop #if equal print
  26.  
  27. min: #min for finding minimum from rest of array
  28. addq $8,%rdi
  29. cmpq $72,%rdi #if reach end of array
  30. jge swap
  31. cmpq %rax,arr(,%rdi) #if this element less than current minimum
  32. jl assign
  33. jmp min
  34.  
  35. swap : #swap function move minimum element to current index
  36. movq arr(,%rdx),%rax
  37. movq arr(,%rsi),%rbx
  38. movq %rax,arr(,%rsi)
  39. movq %rbx,arr(,%rdx)
  40. addq $8,%rdx
  41. jmp sort #jump to sort function again
  42.  
  43. assign: #this label assign minimum to rax
  44. movq %rdi,%rsi
  45. movq arr(,%rdi),%rax
  46. movq %rdi,%rbx
  47. jmp min
  48.  
  49.  
  50. printloop : #this label print every element of sorted array
  51. xorq %rcx,%rcx
  52. movq $0,num
  53. gameon :
  54. movq $1, %rax # syscall for write
  55. movq $msg, %rsi # moving buffer to rsi
  56. movq $1, %rdi # stdout
  57. movq $1, %rdx # length of string
  58. syscall
  59. movq num,%rcx
  60. movq arr(,%rcx),%rax #move element to be printed to rax
  61. addq $8,%rcx
  62. movq %rcx,num
  63. cmpq $80,%rcx
  64. jge bye #if all element is printed jump to exit
  65. jmp print
  66.  
  67. print: # print label to print integer using stack
  68. xorq %rsi, %rsi # rsi reg will contain number of digit in integer
  69.  
  70. loop2: # loop that push each digit in stack from last to front
  71. movq $0, %rdx
  72. movq $10, %rbx
  73. idivq %rbx # rax <- rax/rbx && rdx <- rax % rbx
  74. addq $48, %rdx # converting digit to char
  75. pushq %rdx # push digit in stack
  76. incq %rsi # increment digit count
  77. cmpq $0, %rax # compare rax with zero
  78. jz next # if zero jump to next label
  79. jmp loop2 # else jump to loop label again
  80.  
  81. next: # this label extract each digit from stack and print
  82. cmpq $0, %rsi # print until result is not printed
  83. je gameon
  84. decq %rsi
  85. movq %rsi,%rbx
  86. movq $1, %rax # syscall for write
  87. movq %rsp, %rsi # moving buffer to rsi
  88. movq $1, %rdi # stdout
  89. movq $1, %rdx # length of string
  90. syscall # call to interrupt to print
  91. movq %rbx,%rsi
  92. addq $8, %rsp # rsp <- rsp + 8
  93. jmp next
  94.  
  95. bye: # label for exit
  96. movq $1, %rax # syscall for write
  97. movq $nline, %rsi # moving buffer to rsi
  98. movq $1, %rdi # stdout
  99. movq $1, %rdx # length of string
  100. syscall
  101. movq $60,%rax # syscall for exit
  102. movq $0, %rbx
  103. syscall # calling interrupt to exit
Add Comment
Please, Sign In to add comment