; intlogic.asm show some simple C code and corresponding nasm code ; the nasm code is one sample, not unique ; ; compile: nasm -f elf -l intlogic.lst intlogic.asm ; link: gcc -o intlogic intlogic.o ; run: intlogic ; ; the output from running intlogic.asm and intlogic.c is ; c=5 , a=3, b=5, c=15 ; c=a&b, a=3, b=5, c=1 ; c=a|b, a=3, b=5, c=7 ; c=a^b, a=3, b=5, c=6 ; c=~a , a=3, b=5, c=-4 ; ;The file intlogic.c is: ; /* intlogic.c */ ; #include ; int main() ; { ; int a=3, b=5, c; ; ; c=15; ; printf("%s, a=%d, b=%d, c=%d\n","c=5 ", a, b, c); ; c=a&b; /* and */ ; printf("%s, a=%d, b=%d, c=%d\n","c=a+b", a, b, c); ; c=a|b; /* or */ ; printf("%s, a=%d, b=%d, c=%d\n","c=a-b", a, b, c); ; c=a^b; /* xor */ ; printf("%s, a=%d, b=%d, c=%d\n","c=a*b", a, b, c); ; c=~a; /* not */ ; 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 %macro pabc 1 ; a "simple" print macro section .data .str db %1,0 ; %1 is first actual in macro call section .text ; push onto stack backwards push dword [c] ; int c push dword [b] ; int b push dword [a] ; int a push dword .str ; users string push dword fmt ; address of format string call printf ; Call C function add esp,20 ; pop stack 5*4 bytes %endmacro section .data ; preset constants, writeable a: dd 3 ; 32-bit variable a initialized to 3 b: dd 5 ; 32-bit variable b initializes to 4 fmt: db "%s, a=%d, b=%d, c=%d",10,0 ; format string for printf section .bss ; unitialized space c: resd 1 ; reserve a 32-bit word section .text ; instructions, code segment global main ; for gcc standard linking main: ; label lit5: ; c=5; mov eax,15 ; 5 is a literal constant mov [c],eax ; store into c pabc "c=5 " ; invoke the print macro andb: ; c=a&b; mov eax,[a] ; load a and eax,[b] ; and with b mov [c],eax ; store into c pabc "c=a&b" ; invoke the print macro orw: ; c=a-b; mov eax,[a] ; load a or eax,[b] ; logical or with b mov [c],eax ; store into c pabc "c=a|b" ; invoke the print macro xorw: ; c=a^b; mov eax,[a] ; load a xor eax,[b] ; exclusive or with b mov [c],eax ; store result in c pabc "c=a^b" ; invoke the print macro notw: ; c=~a; mov eax,[a] ; load c not eax ; not, complement mov [c],eax ; store result into c pabc "c=~a " ; invoke the print macro mov eax,0 ; exit code, 0=normal ret ; main returns to operating system