-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic-assembly-examples
More file actions
223 lines (171 loc) · 3.42 KB
/
basic-assembly-examples
File metadata and controls
223 lines (171 loc) · 3.42 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
Structure of an Assembly
.data ; all initialized data
.bss ; all uninitialized data
.text ; program instructions
_start: ; Main() routine
.globl _start ; externally callable routines
@
staction .text
_start:
mov ecx, 99 ; set ecx to 99
mov ebx, 42 ; exit status is 42
mov eax, 1 ; sys_exit system call
cmp ecx, 100 ; compare ecx to 100
jl skip ; jump if less than
mov ebx, 13 ; exit status is 13
skip:
int 0x80
>>42
@
staction .text
_start:
mov ecx, 101 ; set ecx to 101
mov ebx, 42 ; exit status is 42
mov eax, 1 ; sys_exit system call
cmp ecx, 100 ; compare ecx to 100
jl skip ; jump if less than
mov ebx, 13 ; exit status is 13
skip:
int 0x80
>>13
@
global _start
section .text
_start:
mov ebx, 1 ; start ebx at 1
mov ecx, 4 ; number of iterations
label:
add ebx, ebx ; ebx+=ebx
dec ecx ; ecx -=1
cmp ecx, 0 ; compare ecx with 0
jg label ; jump to label if greater
mov eax, 1 ; sys_exit syscam call
int 0x80
>>16
@
global _start
section .data
addr db "yellow"
section .text
_start:
mov eax, 4 ; sys_write system call
mov ebx, 1 ; stdout file descriptor
mov ecx, addr ; bytes to write
mov edx, 6 ; number of bytes to write
int 0x80 ; perform system call
mov eax, 1 ; sys_exit system call
mov ebx, 0 ; exit status is 0
int 0x80
>>yellow
@
global _start
section .data
addr db "yellow"
section .text
_start:
mov [addr], byte 'H'
mov [addr+5], byte '!'
mov eax, 4 ; sys_write system call
mov ebx, 1 ; stdout file descriptor
mov ecx, addr ; bytes to write
mov edx, 6 ; number of bytes to write
int 0x80 ; perform system call
mov eax, 1 ; sys_exit system call
mov ebx, 0 ; exit status is 0
int 0x80
>>Hello!
@
global _start
_start:
sub esp, 4
mov [esp], byte 'H'
mov [esp+1], byte 'e'
mov [esp+2], byte 'y'
mov [esp+3], byte '!'
mov eax, 4 ; sys_write system call
mov ebx, 1 ; stdout file descriptor
mov ecx, esp ; pointer to bytes to write
mov edx, 4 ; number of bytes to write
int 0x80 ; perform system call
mov eax, 1 ; sys_exit system call
mov ebx, 0 ; exit status is 0
int 0x80
>>Hey!
@
global _start
_start:
call func
mov eax, 1
int 0x80
func:
mov ebx, 42
pop eax
jmp eax
>>42
@
global _start
_start:
call func
mov eax, 1
int 0x80
func:
mov ebx, 42
ret
>>42
@
func:
push ebp
mov ebp, esp
sub esp, 2
mov[esp], byte 'H'
mov[esp+1], byte 'i'
mov eax, 4 ; sys_write system call
mov ebx, 1 ; stdout file descriptor
mov ecx, esp ; bytes to write
mov edx, 2 ; number of bytes to write
int 0x80 ; perform system call
mov esp, ebp
pop ebp
ret
>>Hi
@
mov ebx, 123 ; ebx = 123
mov eax, ebx ; eax = ebx
add ebx, ecx ; ebx +=ecx
sub ebx, edx ; ebx-=edx
mul ebx ; eax*=ebx
div edx ; eax /=ebx
@
EAX
------------------- 32 bit
AX
----------- 16 bit
AH AL
----- ----- 8 bit
EBX
------------------- 32 bit
BX
----------- 16 bit
BH BL
----- ----- 8 bit
@
Arguments to syscalls:
EAX -System call number
EBX -First argument
ECX -Second argument
EDX -Third argument
ESI -Fourth argument
EDI -Fifth argument
@
EDI is used for writing
ESI is used for reading
EBX is used for extra storage
ESP register points to the very top of the stack and so it will point to the return address.
@
je A, B ; Jump if Equal
jne A, B ; Jump if not Equal
jg A, B ; Jump if Greater
jge A, B ; Jump Greater or Equal
jl A, B ; Jump if Less
jle A, B ; Jump if Less or Equal
@