-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.s
131 lines (99 loc) · 1.91 KB
/
game.s
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
; Ubuntu mainline kernel version 5.4.8, NASM
global main
extern printf
extern scanf
extern fflush
extern srand
extern rand
extern time
section .data
; str
sft db "%u", 0
prolog db "Guess the number between 0 and 100", 0xa, 0
greater db "The number is greater than %u", 0xa, 0
less db "The number is less than %u", 0xa, 0
remain db "%u guesses remaining: ", 0
winner db "You win! The number was %u", 0xa, 0
loser db "You lose! The number was %u", 0xa, 0
; int
nguess db 5,0,0,0 ; little endian hack (5 guesses)
section .bss
; int
guess resb 4
answer resb 4
section .text
main:
sub rsp, 8
xor rax, rax
xor rdi, rdi
call time
mov rdi, rax
call srand
call rand ; generate random number
xor rdx, rdx
mov rcx, 100
div rcx
mov [answer], rdx
xor rax, rax
mov rdi, prolog
call printf WRT ..plt ; Initial print call
xor edi, edi
call fflush ; flush output
check:
push rbp
call ncheck
pop rbp
cmp rax, 0
je WIN
mov rbx, [nguess]
dec rbx
cmp rbx, 0
jle LOSE ; Out of guesses, loser
mov [nguess], rbx
cmp rax, 0
jne check
add rsp, 8
ret
; returns -1 if >, 0 if =, 1 if <, rax
ncheck:
xor rax, rax
mov rdi, remain
mov DWORD rsi, [nguess]
call printf WRT ..plt
xor rax, rax
mov rdi, sft ; int buffer
mov rsi, guess
call scanf
mov rax, [guess]
cmp eax, [answer] ; compare the guess (lower 4 bits) to the correct number
; setup print call
mov rax, 0 ; xor rax, rax will set 0 flag and render the cmp useless
mov rsi, [guess]
jl _LT
jg _GT
xor rax, rax ; if the user guesses correctly they reach this point
ret
; Called by ncheck
_LT:
mov rdi, greater
call printf WRT ..plt
mov rax, -1
ret
_GT:
mov rdi, less
call printf WRT ..plt
mov rax, 1
ret
; Called by main
WIN:
xor rax, rax
mov rdi, winner
mov DWORD rsi, [answer]
call printf WRT ..plt
ret
LOSE:
xor rax, rax
mov rdi, loser
mov DWORD rsi, [answer]
call printf WRT ..plt
ret