[英]Linked assembly subroutine doesn't work as expected
I'm writing a simple subroutine in FASM to print 32-bit unsigned integers to STDOUT. 我正在用FASM编写一个简单的子例程,以将32位无符号整数打印到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
. 但是,在执行程序时,它仅显示7
而不是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.
这很奇怪,因为我的
uprint
子例程正确。
In fact if I combine both these programs into a single program then it displays
1337
correctly.
实际上,如果我将这两个程序组合到一个程序中,则它将正确显示
1337
。
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. 我正在使用test eax, 0
始终设置零标志。 Hence only the first digit is processed. 因此,仅处理第一个数字。 Intead I need to use either test eax, eax
or cmp eax, 0
. Intead我需要使用test eax, eax
或cmp eax, 0
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.