gpt4 book ai didi

c - 汇编中的 C 程序

转载 作者:行者123 更新时间:2023-11-30 21:30:12 25 4
gpt4 key购买 nike

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

我做错了什么?请尽可能清楚地为我解释一下。我正在使用 gcc,并且正在 intel_syntax Assembly 中编写。

这是我的代码:

    .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"

提前谢谢

最佳答案

使用atoi在这种情况下没有任何意义。您确定应该使用它吗?

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

顺便说一句,您正在破坏 atoi 的返回值通过pop eax 。另外,您还缺少 retmain 末尾.

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

关于c - 汇编中的 C 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27486542/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com