c - 将 C 代码翻译成汇编

标签 c assembly x86 masm irvine32

我需要将这段 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 的 Assembly Language for x86 processors 第 6 版书籍

在这里更新我试图从回答者的回答之一写入 MASM 的代码 我一直收到错误。就像我在使用 Kip Irvine 的汇编语言之前所说的那样,所以我必须包含库链接 INCLUDE Irvine32.inc

this is the error>>>> programb.obj : error LNK2019: unresolved external symbol _scanf referenced in function _main@0

包括 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

对于汇编语言代码我很抱歉....我不知道如何格式化它以便于阅读....

最佳答案

您可以使用 -S 标志给 gcc 生成汇编代码: gcc myfile.c -S -o myfile.s

我的意思是这个程序集文件应该可以回答您所有的问题。

关于c - 将 C 代码翻译成汇编,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4104287/

相关文章:

performance - 依赖链分析

gcc - 是什么导致 "x.asm:(.text+0xd): undefined reference to ` y'”?

gcc - 栈分配,为什么要多出空间?

c - 使用 malloc() 和 sizeof() 函数时出错

c - 为什么SW4STM32 subdir.mk会生成错误?

c - 仅使用现有代码执行任意代码

assembly - MASM:.IF 与有符号数字比较

无法打开设备 eth0 : eth0: socket: Invalid argument

c - 为什么指向 char 数组的指针在右值之间不同是 &"hello"和 char 数组?

winapi - WriteFile字符串字节长度导致崩溃