【问题标题】:Procedures from C in assembly汇编中的 C 程序
【发布时间】:2014-12-15 14:42:33
【问题描述】:

我必须在汇编中编写一个简短的程序,但我的版本不起作用。 它应该打印 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"

提前谢谢你

【问题讨论】:

  • 此类问题应使用正确的架构进行标记。另外,你卡在哪里了?正如这里所写,这不是一个问题。
  • 提供更多信息。问题是什么 ?您使用的是什么编译器/汇编器?什么是架构?您使用的是什么操作系统?根据您的问题,这可能会发生很大变化。猜测可能很累...
  • 我更正了我的问题。对此我很抱歉,但我是大会的新手
  • 你还没有说出你的问题。你能编译这个还是在运行时崩溃?你得到什么错误?了解目标 (gcc -v) 和操作系统也会有所帮助。
  • 它有效,但不像我写的那样正确。

标签: c assembly x86 gnu-assembler intel-syntax


【解决方案1】:

在这种情况下,使用 atoi 毫无意义。你确定你应该使用它吗?

假设您想打印 ascii 代码(对于 a97),您可以改用 printf 或非标准的 itoa

顺便说一句,您正在通过pop eax 破坏来自atoi 的返回值。此外,您缺少main 末尾的ret

使用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"

【讨论】:

  • 是的,我应该只使用 atoi 和 puts。因为我的下一部分任务是将 char '5' 转换为整数,然后对其进行算术运算。
猜你喜欢
  • 2018-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多