-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathfirst_and_last_odd.asm
More file actions
87 lines (75 loc) · 1.16 KB
/
first_and_last_odd.asm
File metadata and controls
87 lines (75 loc) · 1.16 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
; Write an assembly program that reads 10 numbers from the user and prints the first and
; last odd number in the list.
segment .data
a: dq 2
b: dq 0
ind: dq 0
cnt: dq 0
fmt: dq "%lld ",10,0
fmt_in: dq "%lld", 0
fmt_out_first: dq "The first odd is: %lld ", 10, 0
fmt_out_last: dq "The last odd is: %lld", 10, 0
segment .bss
array resq 21
segment .text
global main
extern printf
extern scanf
main:
push RBP
mov RAX, 0
mov RCX, 0
mov RBX, 0
INPUT_ARRAY:
cmp RCX, 10
jz DONE
mov [cnt], RCX
mov RAX, 0
mov RDI, fmt_in
mov RSI, a
call scanf
mov RAX, [a]
mov RCX, [cnt]
mov [array+RCX*8], RAX
add RBX, [a]
inc RCX
jmp INPUT_ARRAY
DONE:
mov RAX, 0
mov RCX, 0
mov RBX, 0
FIRST_ODD:
cmp RCX, 10
jz END
mov RAX, [array+RCX*8]
inc RCX
mov [cnt], RCX
test RAX, 1
jnz PRINT_FIRST_ODD
mov RCX, [cnt]
jmp FIRST_ODD
PRINT_FIRST_ODD:
mov RDI, fmt_out_first
mov RSI, RAX
call printf
mov RAX, 0
mov RCX, 9
mov RBX, 0
LAST_ODD:
cmp RCX, 0
jnge END
mov RAX, [array+RCX*8]
dec RCX
mov [cnt], RCX
test RAX, 1
jnz PRINT_LAST_ODD
mov RCX, [cnt]
jmp LAST_ODD
PRINT_LAST_ODD:
mov RDI, fmt_out_last
mov RSI, RAX
call printf
END:
mov RAX, 0
pop RBP
ret