简体   繁体   中英

Linked assembly subroutine doesn't work as expected

I'm writing a simple subroutine in FASM to print 32-bit unsigned integers to STDOUT. This is what I came up with:

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:

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?

我收获了鲜明的印象,你的链接操作正在建设的uprint的前_start和你其实进入UPRINT ,而不是在_start如您所愿。

I found out my mistake. I'm using test eax, 0 which always sets the zero flag. Hence only the first digit is processed. Intead I need to use either test eax, eax or cmp eax, 0 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM