将 C 代码翻译为汇编
我需要将此 C 代码转换为汇编语言代码,
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int answer, i;
int right, wrong;
right = 0;
wrong = 0;
for(i =1; i < 11; i = i+1){
printf("What is %d + %d? ", i,i);
scanf( "%d", &answer);
if(answer == i + 1) {
printf("right! ");
right++;
}
else {
printf("Sorry, you're wrong. ");
printf("The answer is %d. ", i + 1);
wrong++;
}
}
printf("You got %d right and %d wrong. ", right, wrong );
return 0;
}
我实际上只需要知道如何将变量与汇编语言中的字符串组合起来,就像上面的 C 代码一样。我想我可以处理其他一切。有人可以告诉我吗。我是否必须使用某种参考[]。
注意,我正在使用 MASM 并根据 Kip Irvine 的《x86 处理器汇编语言》第 6 版书籍进行工作
更新这里是我尝试从答案之一的答案中写入 MASM 的代码 > 我不断收到错误消息。就像我之前说过的,我使用 Kip Irvine 的汇编语言,所以我必须包含库链接 INCLUDE Irvine32.inc
<块引用> <块引用>这是错误>>>>> programb.obj:错误LNK2019:函数_main@0中引用的无法解析的外部符号_scanf
包括 Irvine32.inc
有人可以帮我解决这个问题吗
.data
string1 db "What is %d + %d? ", 0
string2 db "%d", 0
string3 db "right! ", 0
string4 db "Sorry, you're wrong. The answer is %d", 10, 0
string5 db "You got %d right and %d wrong.", 10, 0
answer dd 0
right dd 0
wrong dd 0
.code
main PROC
mov ebx, 1
L1:
cmp ebx, 11
je L2
push 1
push ebx
mov edx,OFFSET string1
call WriteString
add esp, 12
push answer
mov edx,OFFSET string2
call scanf
add esp, 8
inc ebx
cmp answer, ebx
jne L3
push ebx
mov edx,OFFSET string3
call WriteString
add esp, 8
inc right
jmp L1
L3:
push ebx
mov edx,OFFSET string4
call WriteString
add esp, 8
inc wrong
jmp L1
L2:
push wrong
push right
mov EDX,OFFSET string5
call WriteString
add esp, 12
exit
main ENDP
END main
programb.obj : error LNK2019: unresolved external symbol _scanf referenced in function _main@0
?我对汇编语言代码感到抱歉......我不知道如何格式化它这样可以更容易阅读......
I need to translate this C code to assembly language code
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int answer, i;
int right, wrong;
right = 0;
wrong = 0;
for(i =1; i < 11; i = i+1){
printf("What is %d + %d? ", i,i);
scanf( "%d", &answer);
if(answer == i + 1) {
printf("right! ");
right++;
}
else {
printf("Sorry, you're wrong. ");
printf("The answer is %d. ", i + 1);
wrong++;
}
}
printf("You got %d right and %d wrong. ", right, wrong );
return 0;
}
I really just need to know how to combine a variable with a string in assembly language like in the above C code. I think I can handle everything else. Could somebody tell me. Would I have to use some kind of reference[].
Note I'm using MASM and working out of Kip Irvine's Assembly Language for x86 processors 6th edition book
update heres the code I attempted to write over to MASM from one of the answerer's answer I keep getting a error. Like I said before I'm using Kip Irvine's Assembly Language so I have to include the library link INCLUDE Irvine32.inc
this is the error>>>> programb.obj : error LNK2019: unresolved external symbol _scanf referenced in function _main@0
INCLUDE Irvine32.inc
can somebody help me get this right
.data
string1 db "What is %d + %d? ", 0
string2 db "%d", 0
string3 db "right! ", 0
string4 db "Sorry, you're wrong. The answer is %d", 10, 0
string5 db "You got %d right and %d wrong.", 10, 0
answer dd 0
right dd 0
wrong dd 0
.code
main PROC
mov ebx, 1
L1:
cmp ebx, 11
je L2
push 1
push ebx
mov edx,OFFSET string1
call WriteString
add esp, 12
push answer
mov edx,OFFSET string2
call scanf
add esp, 8
inc ebx
cmp answer, ebx
jne L3
push ebx
mov edx,OFFSET string3
call WriteString
add esp, 8
inc right
jmp L1
L3:
push ebx
mov edx,OFFSET string4
call WriteString
add esp, 8
inc wrong
jmp L1
L2:
push wrong
push right
mov EDX,OFFSET string5
call WriteString
add esp, 12
exit
main ENDP
END main
programb.obj : error LNK2019: unresolved external symbol _scanf referenced in function _main@0
I'm sorry about the assembly language code....I don't know how to format it so it can be easier to read....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 gcc 的
-S
标志来生成汇编代码:gcc myfile.c -S -o myfile.s
我的意思是这个汇编文件应该回答您的所有问题。
You can use the
-S
flag to gcc to produce the assembly code:gcc myfile.c -S -o myfile.s
What I mean is that this assembly file should answer all your questions.
我很无聊所以我为你做了这个。我使用 NASM,而不是 MASM。我假设 EBX 是被调用者保存的寄存器。代码不是特别好。 :-)
I was bored so I did this for you. I used NASM, rather than MASM. I assumed that EBX is a callee-saved register. The code is not particularly good. :-)
幸运的是,
printf
函数几乎可以为您完成所有操作,甚至包括汇编。您可能已经了解了堆栈,以及如何调用采用已压入堆栈的参数的函数。printf
也是如此。以相反的顺序压入参数,以便堆栈顶部是对格式字符串的引用。那么您所要做的就是:如果我没记错的话, printf 知道它至少有一个参数,并且第一个参数(格式字符串)是堆栈指针指向的参数。因此,
printf
将扫描格式字符串并检查它是否需要替换任何其他参数,例如 i 和 i+1。再说一次,printf
正在做这件事,你不需要担心它。希望这有帮助!
PS 回复:前面的答案,如果您想学习汇编,通常查看编译器生成的汇编代码没有帮助。即使没有启用优化,输出也不适合人类阅读。
Luckily for you, the
printf
function will do almost everything for you, even from assembly. You've probably read about the stack, and how you can call functions that take arguments that have been pushed on the stack. The same is true ofprintf
. Push on the arguments in reverse order, so that the top thing on the stack is a reference to the format string. Then all you have to do is:If I remember correctly,
printf
knows it has at least one argument, and that first argument (the format string) is the one that the stack pointer is pointing to. So thenprintf
will scan through the format string and check if it needs to substitute in any of your other arguments, like i and i+1. Again,printf
's doing this, you don't need to worry about it.Hope this helps!
P.S. Re: the previous answers, usually it's not helpful to look at compiler-generated assembly code if you're trying to learn assembly. Even without optimizations enabled, the output's not meant for humans to read.
例如,C 编程语言中的这一行:
相当于:
As an example , this line in C programming language:
is equivalent to: