NASM
NASM can be used in combinations of the following With C/C++ to dene functions that can be used by C/C++ To construct a program in assembly language only To construct a program in assembly language which calls C functions The mechanisms to do this depend on the compiler. We will consider the GNU Compiler Collection (GCC).
add: ; [esp] is the return address, ; [esp+4] the first parameter, etc. mov eax, [esp+4] add eax, [esp+8] ret
5
factorial: _factorial: push ebp mov ebp, esp push ecx ;;; start function mov ecx, [ebp+8] mov eax, 1 mainloop: cmp ecx, 0 jz done mul ecx dec ecx jmp mainloop done: pop ecx pop ebp ret
; save base pointer ; store stack pointer ; save ecx ; ecx = first argument ; eax = 1 ; if(ecx == 0) ; goto done ; else eax = eax * ecx ; ecx = ecx - 1
11
12
message: db "output some text", 10, 0 use_printf: _use_printf: push message call printf pop eax ; first parameter ret
13
14
15
; save registers
; remove parameters ; address of integer1 (second parameter) ; arguments are right to left (first parameter) ; remove parameters
push message2 call printf add esp, 4 push push call add integer2 formatin scanf esp, 8
; remove parameters ; address of integer2 ; arguments are right to left ; remove parameters 17
18
19
20
21
; save registers
; argc
mov eax, [ebx+4] call atoi mov ecx, eax mov eax, [ebx+8] call atoi add eax, ecx
main_end: pop ecx pop ebx pop ebp mov eax, 0 ret SECTION .data
24
25
26
27
; ; ; ;
PUSH DX MOV AH, 09h ; write string MOV DX, clear_line INT 21h POP DX
29
; hour
XOR AX, AX MOV AL, CL CALL show_integer MOV MOV INT AH, 02h DL, : 21h
; minute
; second
30
31
; empty string
; write string
33