This repository has been archived by the owner on Feb 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.asm
145 lines (121 loc) · 2.2 KB
/
main.asm
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
SYS_EXIT equ 1
SYS_READ equ 3
SYS_WRITE equ 4
STDIN equ 0
STDOUT equ 1
STDERR equ 2
section .data
oddMsg db "Your number is odd", 0xA, 0x0
oddLen equ $ - oddMsg
evenMsg db "Your number is even", 0xA, 0x0
evenLen equ $ - evenMsg
emptyMsg db "Your input is empty", 0xA, 0x0
emptyLen equ $ - emptyMsg
askNumMsg db "Please enter a number: ", 0x0
askNumLen equ $ - askNumMsg
displayNumMsg db "You entered ", 0x0
displayNumLen equ $ - displayNumMsg
containsLetterMsg db "Your input contains a letter", 0xA, 0x0
containsLetterLen equ $ - containsLetterMsg
unexpectedErrorMsg db "Unexpected error occurred", 0xA, 0x0
unexpectedErrorLen equ $ - unexpectedErrorMsg
section .bss
input resb 8
exitCode resb 1
section .text
global _start
_start:
mov rax, SYS_WRITE
mov rbx, STDOUT
mov rcx, askNumMsg
mov rdx, askNumLen
int 80h
mov rax, SYS_READ
mov rbx, STDIN
mov rcx, input
mov rdx, 8
int 80h
mov rax, SYS_WRITE
mov rbx, STDOUT
mov rcx, displayNumMsg
mov rdx, displayNumLen
int 80h
mov rax, SYS_WRITE
mov rbx, STDOUT
mov rcx, input
mov rdx, 8
int 80h
mov r8, 0
inputLoop:
cmp byte[input+r8], 0x0
je unexpectedError
cmp byte[input+r8], 0xA
je end
cmp byte[input+r8], 0x30
jl containsLetters
cmp byte[input+r8], 0x39
jg containsLetters
inc r8
jmp inputLoop
end:
dec r8
cmp r8, -1
je empty
cmp byte[input+r8], 0x30
je even
cmp byte[input+r8], 0x32
je even
cmp byte[input+r8], 0x34
je even
cmp byte[input+r8], 0x36
je even
cmp byte[input+r8], 0x38
je even
jmp odd
odd:
mov rax, SYS_WRITE
mov rbx, STDOUT
mov rcx, oddMsg
mov rdx, oddLen
int 80h
jmp success
even:
mov rax, SYS_WRITE
mov rbx, STDOUT
mov rcx, evenMsg
mov rdx, evenLen
int 80h
jmp success
empty:
mov rax, SYS_WRITE
mov rbx, STDERR
mov rcx, emptyMsg
mov rdx, emptyLen
int 80h
jmp fail
containsLetters:
mov rax, SYS_WRITE
mov rbx, STDERR
mov rcx, containsLetterMsg
mov rdx, containsLetterLen
int 80h
jmp fail
unexpectedError:
mov rax, SYS_WRITE
mov rbx, STDERR
mov rcx, unexpectedErrorMsg
mov rdx, unexpectedErrorLen
int 80h
jmp fail
success:
mov r9, 0
mov [exitCode], r9
jmp exit
fail:
mov r9, 1
mov [exitCode], r9
jmp exit
exit:
mov rax, SYS_EXIT
mov rbx, [exitCode]
int 80h