flat assembler
Message board for the users of flat assembler.

Index > Linux > Linked subroutine doesn't work as expected

Author
Thread Post new topic Reply to topic
aaditmshah



Joined: 21 Apr 2013
Posts: 5
Location: Bloomington
aaditmshah
I'm writing a simple subroutine to print 32-bit unsigned integers to STDOUT. This is what I came up with:

Code:
format elf
public uprint

section ".text" executable
uprint:
    push ebx
    push ecx
    push edx
    push esi
    mov  ebx, 10
    mov  ecx, buf + 11
    xor  esi, esi
do:
    dec ecx
    xor edx, edx
    div ebx
    add dl, 0x30
    mov [ecx], dl
    inc esi
    test eax, 0
    jnz do
    mov eax, 4
    mov ebx, 1
    mov edx, esi
    int 0x80
    pop esi
    pop edx
    pop ecx
    pop ebx
    ret

section ".data" writeable
    buf rb 11    


Then I wrote another program to test whether the above subroutine works properly:

Code:
format elf
extrn uprint
public _start

section ".text" executable
_start:
    mov eax, 1337
    call uprint
    mov eax, 4
    mov ebx, 1
    mov ecx, newline
    mov edx, 1
    int 0x80
    mov eax, 1
    xor ebx, ebx
    int 0x80

section ".data"
    newline db 0x0A    


I compiled both these programs to their corresponding object files and linked them to create the executable.

On executing the program however it only displayed 7 instead of 1337. As it turns out only the last digit of the number is display regardless of the number itself.

This is strange because my uprint subroutine is correct. In fact if I combine both these programs into a single program then it displays 1337 correctly.

What am I doing wrong?
Post 21 Apr 2013, 07:57
View user's profile Send private message Send e-mail Visit poster's website Reply with quote
Turbo Lover



Joined: 22 Feb 2013
Posts: 32
Turbo Lover
the problem is in the uprint routine.
Post 21 Apr 2013, 11:26
View user's profile Send private message Reply with quote
aaditmshah



Joined: 21 Apr 2013
Posts: 5
Location: Bloomington
aaditmshah
Turbo Lover wrote:
the problem is in the uprint routine.


I can't seem to find where the problem lies in the uprint routine. Would you help me to isolate it, please?
Post 21 Apr 2013, 15:30
View user's profile Send private message Send e-mail Visit poster's website Reply with quote
aaditmshah



Joined: 21 Apr 2013
Posts: 5
Location: Bloomington
aaditmshah
I found the problem. In my uprint routine I'm executing `test eax, 0` which always sets the zero flag. Hence only the first digit is processed. I need to use either `test eax, eax` or `cmp eax, 0` instead.
Post 21 Apr 2013, 16:09
View user's profile Send private message Send e-mail Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar.

Powered by rwasa.