-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxArray.s
More file actions
72 lines (63 loc) · 3.38 KB
/
MaxArray.s
File metadata and controls
72 lines (63 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Assembly Program to calculate max of array of number using AT&T syntax
# Author : Ayush Mishra, Roll Number : 1501CS16
# Data directive : initialize data section
.data
s : .quad -3,2,7,5,12,3,6
msg : .string "\n"
# Text directive : Executable code Section
.text
.globl _start
_start: # starting point of Program
xorq %rax,%rax # initialize rax <- 0 ,it will contain max
movq s,%rax # move value of s[0] to rax
movq $6,%rcx # move remaining length of array to traverse
movq $8,%rdx # rdx contain size of quad i.e 8 byte
findmax : # this label find maximum in array
cmpq s(,%rdx),%rax # accesing s[i]
jl assign # if s[i] > rax(current max) change current max
addq $8,%rdx # add 8 byte in rdx as it will be next valid address
decq %rcx # rcx work as counter for array decrement rcx
cmpq $0,%rcx # if array is traversed
je print # jump to print max
jmp findmax # else again findmax
assign : # this label update current max
movq s(,%rdx),%rax
addq $8,%rdx
decq %rcx
cmpq $0,%rcx
je print
jmp findmax
print: # print label to print integer using stack
xorq %rsi, %rsi # rsi reg will contain number of digit in integer
loop: # 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 loop # 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 bye
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 : # this label is to exit from program
movq $1, %rax # syscall for write
movq $msg, %rsi # moving buffer to rsi
movq $1, %rdi # stdout
movq $1, %rdx # length of string
syscall # call to interrupt to print
movq $60,%rax # syscall for exit
movq $0,%rdi
syscall # calling interrupt to exit