将十进制值打印到控制台

时间:2014-12-11 19:37:56

标签: assembly intel

我之前曾经问过类似的问题,但我不知道我到底做错了什么:D

我试图在Ubuntu机器上使用AT& T语法在汇编程序中将小数转换为字符串(不确定那是否重要lol)

无论如何,这是我的代码:

.code32     #use 32bit

.section .data

str:    .ascii  "Hello world!\n"
    strlen = . - str

.section .text

.global _start

_start:
    movl    $4, %eax        #4 = sys_write
    movl    $1, %ebx        #1 = console
    movl    $str, %ecx      #address of string to print
    movl    $strlen, %edx   #length of string
    int     $0x80           #sys_call

    movl    $1337, %eax #move some random number to eax
    xorl    %esi, %esi     #clear esi

loop:
    movl    $0, %edx
    movl    $10, %ebx
    divl    %ebx           #divide our number by 10... result will be in eax, remainder in edx
    addl    $48, %edx       #add 48 (ascii for '0') to convert our number to ascii
    pushl   %edx           #push ascii of our number onto stack
    addl    $1, %esi        #counter++
    cmpl    $0, %eax        #if result of division is 0, we are done
    jz      next
    jmp     loop

next:
    cmpl    $0, %esi        #if counter is 0, we are done
    jz      exit
    subl    $1, %esi        #counter--
    movl    $4, %eax        #4 = syswrite
    movl    $1, %ebx        #1 = console
    movl    %esp, %ecx     #address of highest byte on stack (the last number we pushed here)
    movl    $1, %edx        #is number 0-9, so only one char needs to be printed
    int     $0x80           #syscall
    addl    $4, esp     #move stackpointer to next byte
    jmp     next

exit:                      #clean exit
    movl    $1, %eax
    movl    $0, %ebx
    int     $0x80

现在问题是这不会打印我的号码:( 打印字符串“Hello world!\ n”工作正常,所有寄存器也正确设置...我已经尝试用gdb调试这个,检查eax,ebx,ecx,edx和esp结尾的实际值“下一个”循环,结果如下:
eax总是4(正确,就像在Hello打印中一样)
ebx总是1(正确,就像在Hello打印中一样)
ecx总是在-8000左右的负数(我假设这是我的堆栈所在的位置)
edx总是1(正确,只需要打印一个字符)
esp的值是:49,51,51,55 ......对应于ascii:1,3,3,7(这是正确的,应该打印)

所以基本上,所有寄存器都设置正确(除了ecx,我不完全确定那个,但很确定:D),但系统调用什么都不做:(

我们还没有学会如何在某个地方创建一个新的字符串以保存我的字符,所以这就是为什么我只是将它们保存在堆栈中,所以请不要因此而激怒我:D

这是大学的练习,所以我不能使用任何外部函数调用。

0 个答案:

没有答案