Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Assembly Program to sort array of number using AT&T syntax
- # Author : Ayush Mishra, Roll Number : 1501CS16
- # Data directive : initialize data section
- .data
- arr : .quad 25,35,99,24,5,1,13,42,34
- num : .space 8
- msg : .string " "
- nline : .string "\n"
- # Text directive : Executable code Section
- .text
- .globl _start
- _start:
- movq $9,%rcx #rcx <- 9 contain no. of element in array
- movq $0,%rdx #rdx <- 0 contain offset of various element
- sort : #sort for sorting array
- movq arr(,%rdx),%rax #rax <- arr+rdx
- decq %rcx
- movq %rdx,%rdi
- movq %rdx,%rsi
- cmpq $0,%rcx #checking whether we reach end of array
- je printloop #if equal print
- min: #min for finding minimum from rest of array
- addq $8,%rdi
- cmpq $72,%rdi #if reach end of array
- jge swap
- cmpq %rax,arr(,%rdi) #if this element less than current minimum
- jl assign
- jmp min
- swap : #swap function move minimum element to current index
- movq arr(,%rdx),%rax
- movq arr(,%rsi),%rbx
- movq %rax,arr(,%rsi)
- movq %rbx,arr(,%rdx)
- addq $8,%rdx
- jmp sort #jump to sort function again
- assign: #this label assign minimum to rax
- movq %rdi,%rsi
- movq arr(,%rdi),%rax
- movq %rdi,%rbx
- jmp min
- printloop : #this label print every element of sorted array
- xorq %rcx,%rcx
- movq $0,num
- gameon :
- movq $1, %rax # syscall for write
- movq $msg, %rsi # moving buffer to rsi
- movq $1, %rdi # stdout
- movq $1, %rdx # length of string
- syscall
- movq num,%rcx
- movq arr(,%rcx),%rax #move element to be printed to rax
- addq $8,%rcx
- movq %rcx,num
- cmpq $80,%rcx
- jge bye #if all element is printed jump to exit
- jmp print
- print: # print label to print integer using stack
- xorq %rsi, %rsi # rsi reg will contain number of digit in integer
- loop2: # loop that push each digit in stack from last to front
- movq $0, %rdx
- movq $10, %rbx
- idivq %rbx # rax <- rax/rbx && rdx <- rax % rbx
- addq $48, %rdx # converting digit to char
- pushq %rdx # push digit in stack
- incq %rsi # increment digit count
- cmpq $0, %rax # compare rax with zero
- jz next # if zero jump to next label
- jmp loop2 # else jump to loop label again
- next: # this label extract each digit from stack and print
- cmpq $0, %rsi # print until result is not printed
- je gameon
- decq %rsi
- movq %rsi,%rbx
- movq $1, %rax # syscall for write
- movq %rsp, %rsi # moving buffer to rsi
- movq $1, %rdi # stdout
- movq $1, %rdx # length of string
- syscall # call to interrupt to print
- movq %rbx,%rsi
- addq $8, %rsp # rsp <- rsp + 8
- jmp next
- bye: # label for exit
- movq $1, %rax # syscall for write
- movq $nline, %rsi # moving buffer to rsi
- movq $1, %rdi # stdout
- movq $1, %rdx # length of string
- syscall
- movq $60,%rax # syscall for exit
- movq $0, %rbx
- syscall # calling interrupt to exit
Add Comment
Please, Sign In to add comment