Académique Documents
Professionnel Documents
Culture Documents
msg: len:
SECTION .data db "Hello World",10 equ $-msg SECTION .text global main mov mov mov mov int mov mov int edx,len ecx,msg ebx,1 eax,4 0x80 ebx,0 eax,1 0x80
data section the string to print, 10=cr "$" means "here" len is a value, not an address
main:
; code section ; make label available to linker ; standard gcc entry point ; ; ; ; ; arg3, length of string to print arg2, pointer to string arg1, where to write, screen write sysout command to int 80 hex interrupt 80 hex, call kernel
; exit code, 0=normal ; exit command to kernel ; interrupt 80 hex, call kernel
Equivalent C code /* printf1.c print an int and an expression */ #include int main() { int a=5; printf("a=%d, eax=%d\n", a, a+2); return 0; }
; Declare some external functions ; extern printf ; the C function, to be called SECTION .data fmt: ; Data section, initialized variables
SECTION .text global main main: push mov mov add push push push call add mov pop mov ret ebp ebp,esp eax, [a] eax, 2 eax dword [a] dword fmt printf esp, 12 esp, ebp ebp eax,0
; Code section. ; the standard gcc entry point ; the program label for the entry point ; set up stack frame ; ; ; ; ; ; put a from store into register a+2 value of a+2 value of variable a address of ctrl string Call C function ; pop stack 3 push times 4 bytes
; takedown stack frame ; same as "leave" op ; normal, no error, return value ; return
db "Hello world: %c %s of length %d %d %X %e %E",10,0 ; format string for printf char1: db 'a' ; a character str1: db "string",0 ; a C string, "string" needs 0 len: equ $-str1 ; len has value, not an address inta1: dd 1234567 ; integer 1234567 hex1: dd 0x6789ABCD ; hex constant flt1: dd 5.327e-30 ; 32-bit floating point flt2: dq -123.456789e300 ; 64-bit floating point
SECTION .bss flttmp: resq 1 SECTION .text global main main: fld fstp push push push push push push push push push push call add mov ret dword [flt1] qword [flttmp] dword [flt2+4] dword [flt2] dword [flttmp+4] dword [flttmp] dword [hex1] dword [inta1] dword len dword str1 dword [char1] dword msg printf esp, 40 eax, 0 ; 64-bit temporary for printing flt1 ; Code section. ; "C" main program ; label, start of main program ; need to convert 32-bit to 64-bit ; floating load makes 80-bit, ; store as 64-bit ; push last argument first ; 64 bit floating point (bottom) ; 64 bit floating point (top) ; 64 bit floating point (bottom) ; 64 bit floating point (top) ; hex constant ; integer data pass by value ; constant pass by value ; "string" pass by reference ; 'a' ; address of format string ; Call C function ; pop stack 10*4 bytes ; exit code, 0=normal ; main returns to operating system
; ; ; ; }
c=c/a; printf("%s, a=%d, b=%d, c=%d\n","c=c/a", a, b, c); return 0; extern printf ; the C function to be called ; a "simple" print macro ; %1 is first actual in macro call ; ; ; ; ; push onto stack backwards int c int b int a users string ; address of format string ; Call C function ; pop stack 5*4 bytes
%macro pabc 1 section .data .str db %1,0 section .text push push push push push call add %endmacro a: b: fmt: c: dword [c] dword [b] dword [a] dword .str dword fmt printf esp,20
section .data ; preset constants, writeable dd 3 ; 32-bit variable a initialized to 3 dd 4 ; 32-bit variable b initializes to 4 db "%s, a=%d, b=%d, c=%d",10,0; format string for printf section .bss resd 1 section .text global main ; unitialized space ; reserve a 32-bit word ; instructions, code segment ; for gcc standard linking ; label ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; c=5; 5 is a literal constant store into c invoke the print macro c=a+b; load a add b store into c invoke the print macro c=a-b; load a subtract b store into c invoke the print macro c=a*b; load a (must be eax for multiply) signed integer multiply by b store bottom half of product into c invoke the print macro c=c/a; load c load upper half of dividend with zero divide double register edx eax by a store quotient into c invoke the print macro
main: lit5:
mov mov pabc addb: mov add mov pabc mov sub mov pabc mov imul mov pabc mov mov idiv mov pabc mov ret
eax,5 [c],eax "c=5 " eax,[a] eax,[b] [c],eax "c=a+b" eax,[a] eax,[b] [c],eax "c=a-b" eax,[a] dword [b] [c],eax "c=a*b" eax,[c] edx,0 dword [a] [c],eax "c=c/a" eax,0
subb:
mulb:
diva:
section .data ; preset constants, writeable dq 3.333333333 ; 64-bit variable a initialized to 3.0 dq 4.444444444 ; 64-bit variable b initializes to 4.0 dq 5.0 ; constant 5.0 db "%s, a=%e, b=%e, c=%e",10,0; format string for printf
c:
; unitialized space ; reserve a 64-bit word ; instructions, code segment ; for gcc standard linking ; label ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; c=5.0; 5.0 constant store into c invoke the print macro c=a+b; load a (pushed on flt pt stack, st0) floating add b (to st0) store into c (pop flt pt stack) invoke the print macro c=a-b; load a (pushed on flt pt stack, st0) floating subtract b (to st0) store into c (pop flt pt stack) invoke the print macro c=a*b; load a (pushed on flt pt stack, st0) floating multiply by b (to st0) store product into c (pop flt pt stack) invoke the print macro c=c/a; load c (pushed on flt pt stack, st0) floating divide by a (to st0) store quotient into c (pop flt pt stack) invoke the print macro
main: lit5:
fld fstp pabc addb: fld fadd fstp pabc fld fsub fstp pabc fld fmul fstp pabc fld fdiv fstp pabc mov ret
qword [five] qword [c] "c=5.0" qword [a] qword [b] qword [c] "c=a+b" qword [a] qword [b] qword [c] "c=a-b" qword [a] qword [b] qword [c] "c=a*b" qword [c] qword [a] qword [c] "c=c/a" eax,0
subb:
mulb:
diva: