samedi 18 avril 2015

NASM 32-bit: printing content of register by printf

I'm new to assembly. I'm having different output for following simple code from what I expected. each time before printf is called, content of eax is shifted to the right by some number. What am I doing wrong? Thanks.


Code:



;file name : testing.asm
;assemble and link with:
;nasm -f elf testing.asm && gcc -m32 -o testing testing.o
extern printf ; the C function, to be called

SECTION .data ; Data section, initialized variables

a: dd 15 ; int a=5;
str: db "content in eax=%d", 10, 0

SECTION .text ; Code section.

global main ; the standard gcc entry point
main: ; the program label for the entry point
mov ebp, esp
mov eax, [a] ; put a from store into register

shr eax, 1 ; eax content should be 15>>1 = 7
push eax
push dword str ; string to be printed
call printf ; printf(str,content_of_eax)

shr eax, 2
push eax ; eax content should be 7>>2 = 1
push dword str
call printf ; printf(str,content_of_eax)

shr eax, 1
push eax ; eax content should be 1>>1 = 0
push dword str
call printf ; printf(str,content_of_eax)

mov esp, ebp ; takedown stack frame
mov eax, 0 ; normal, no error, return value
ret ; return


Output:



content in eax=7
content in eax=4
content in eax=8


Expected Output:



content in eax=7
content in eax=1
content in eax=0

Aucun commentaire:

Enregistrer un commentaire