; shift.asm show some simple C code and corresponding nasm code ; the nasm code is one sample, not unique ; ; compile: nasm -f elf -l shift.lst shift.asm ; link: gcc -o shift shift.o ; run: ./shift ; ; the output from running shift.asm (zero filled) is: ; shl eax,1, old eax=A9876543, new eax=530ECA86, ; shl eax,4, old eax=A9876543, new eax=98765430, ; shr eax,4, old eax=A9876543, new eax=0A987654, ; sal eax,4, old eax=A9876543, new eax=98765430, ; sar eax,4, old eax=A9876543, new eax=FA987654, ; rol eax,4, old eax=A9876543, new eax=9876543A, ; ror eax,4, old eax=A9876543, new eax=3A987654, ; shld edx,eax,4, old edx:eax=0,A9876543, new eax=A9876543 edx=0000000A, ; shl eax,4 , old edx:eax=0,A9876543, new eax=98765430 edx=0000000A, ; shrd edx,eax,4, old edx:eax=0,A9876543, new eax=A9876543 edx=30000000, ; shr eax,4 , old edx:eax=0,A9876543, new eax=0A987654 edx=30000000, extern printf ; the C function to be called %macro prt 1 ; old and new eax section .data .str db %1,0 ; %1 is which shift section .text ; push onto stack backwards push eax ; new eax push dword .str ; users string push dword fmt ; address of format string call printf ; Call C function add esp,12 ; pop stack 3*4 bytes %endmacro %macro prt2 1 ; old and new eax section .data .str db %1,0 ; %1 is which shift section .text ; push onto stack backwards push edx ; new edx push eax ; new eax push dword .str ; users string push dword fmt2 ; address of format string call printf ; Call C function add esp,16 ; pop stack 4*4 bytes %endmacro section .bss eaxsave: resd 1 ; save eax while calling a function edxsave: resd 1 ; save edx while calling a function section .data ; preset constants, writeable fmt: db "%s, old eax=A9876543, new eax=%8X, ",10,0 ; format string fmt2: db "%s, old edx:eax=0,A9876543, new eax=%8X edx=%8X, ",10,0 section .text ; instructions, code segment global main ; for gcc standard linking main: ; label shl1: mov eax,0xA9876543 ; data to shift shl eax,1 ; shift eax one bit position left prt "shl eax,1" ; invoke the print macro shl4: mov eax,0xA9876543 ; data to shift shl eax,4 ; shift eax four bit positions left prt "shl eax,4" ; invoke the print macro shr4: mov eax,0xA9876543 ; data to shift shr eax,4 ; shift prt "shr eax,4" ; invoke the print macro sal4: mov eax,0xA9876543 ; data to shift sal eax,4 ; shift prt "sal eax,4" ; invoke the print macro sar4: mov eax,0xA9876543 ; data to shift sar eax,4 ; shift prt "sar eax,4" ; invoke the print macro rol4: mov eax,0xA9876543 ; data to shift rol eax,4 ; shift prt "rol eax,4" ; invoke the print macro ror4: mov eax,0xA9876543 ; data to shift ror eax,4 ; shift prt "ror eax,4" ; invoke the print macro shld4: mov eax,0xA9876543 ; data to shift mov edx,0 ; register receiving bits shld edx,eax,4 ; shift mov [eaxsave],eax ; save, destroyed by function mov [edxsave],edx ; save, destroyed by function prt2 "shld edx,eax,4"; invoke the print macro shla: mov eax,[eaxsave] ; restore, destroyed by function mov edx,[edxsave] ; restore, destroyed by function shl eax,4 ; finish double shift, both registers prt2 "shl eax,4 "; invoke the print macro shrd4: mov eax,0xA9876543 ; data to shift mov edx,0 ; register receiving bits shrd edx,eax,4 ; shift mov [eaxsave],eax ; save, destroyed by function mov [edxsave],edx ; save, destroyed by function prt2 "shrd edx,eax,4"; invoke the print macro shra: mov eax,[eaxsave] ; restore, destroyed by function mov edx,[edxsave] ; restore, destroyed by function shr eax,4 ; finish double shift, both registers prt2 "shr eax,4 "; invoke the print macro mov eax,0 ; exit code, 0=normal ret ; main returns to operating system