-4

我必须在汇编中编写一个简短的程序,但我的版本不起作用。它应该打印 ASCII 字符,然后通过 atoi 函数将其更改为整数值并打印出该值。重要的是使用 C 中的程序:puts() 和 atoi()。

我做错了什么?请尽可能清楚地为我解释。我正在使用 gcc,并且正在使用 intel_syntax 程序集编写代码。

这是我的代码:

    .intel_syntax noprefix
    .text
    .globl main  

main:

    mov eax, offset msg
    push eax
    call puts
    pop eax

    push eax
    call atoi
    pop eax

    push eax
    call puts
    pop eax

    .data
msg:
    .asciz "a"

先感谢您

4

1 回答 1

0

在这种情况下使用atoi是没有意义的。你确定你应该使用它吗?

假设您想打印 ascii 代码(97用于a),您可以使用printf或非标准代码itoa

顺便说一句,您正在破坏atoi. pop eax此外,您ret缺少main.

示例代码使用printf

main:
    push offset msg
    call puts
    pop eax

    movzx eax, byte ptr [msg]
    push eax
    push offset fmt
    call printf
    add esp, 8   # clean up stack

    xor eax, eax # zero return value
    ret          # return
.data
msg:
    .asciz "a"
fmt:
    .asciz "%d\n"
于 2014-12-15T15:29:28.577 回答