无法打印整数到标准输出(汇编)

时间:2013-12-25 10:15:54

标签: assembly int stdout

我尝试添加一个,然后将总和打印到标准输出。但没有任何东西可以印刷。我读了this tutorial,只是将文本字符串更改为我的16位整数,以存储在寄存器中。

.section .data
  x:   .int 40
  y:   .int 10
  z:   .int 30
  v:   .int 8
  sum: .int 0
  len: .int 2   # 2 bytes integer for length of sum

.section .text

.globl _start
_start:

  movl x, %eax
  addl y, %eax
  addl z, %eax
  subl v, %eax
  movl %eax, sum

 # put sum to standard out

  movl len, %edx        # third argument, length of sum
  movl sum, %ecx        # second argument: pointer to message
  movl $1, %ebx         # first argument: file handle (stdout)
  movl $4, %eax         # system call number (sys_write)

  int $0x80             # call kernel

        # exit

  movl $0, %ebx
  movl $1, %eax
  int  $0x80

1 个答案:

答案 0 :(得分:2)

  

确定!将int转换为字符串时有什么方法吗?

首先(仅用于测试目的)您可以使用用C编写的帮助程序。

您编写一个字节的消息类型和多个字节的数据。帮助程序将转换它们。命令行将如下所示:

assembler_program | helper_program

对于真正的程序,你必须将寄存器值转换为十进制。这是用于无符号数字:

     # Print out the value of eax...
     # Prepare...
    mov %esp, %ecx
    sub $20, %esp
    mov $10, %ebx
     # Convert to decimal
next_digit:
    xor %edx,%edx
    div %ebx
    add $'0', %dl
    dec %ecx
    mov %dl, 0(%ecx)
    test %eax, %eax
    jnz next_digit
     # Calculate the size
    lea 20(%esp), %edx
    sub %ecx, %edx
     # Call int $0x80
    mov $4, %eax
    mov $1, %ebx
    int $0x80
     # Clean up
    add $20, %esp

对于签名号码,您需要提供以下附加代码:

    test %eax,%eax
    jns positive
    neg %eax
    push %eax
    mov $4, %eax
    mov $1, %ebx
    mov $minus, %ecx
    mov $1, %edx
    int $0x80
    pop %eax
positive:
    # Now do the unsigned output from above

minus:
    .byte '-'