Convert C code to assembly

I need to translate this 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 just need to know how to combine the variable with a string in assembly language, as in the above C code. I think I can handle everything else. Can anyone tell me. Should I use some kind of link [].

Note. I use MASM and the development of the Kip Irvine build language for x86 processors. Book of the 6th edition.

update the code that I tried to write to MASM from one of the responder's answers, I get an error all the time. As I said, I use the assembly language Kip Irvine, so I need to include the INCLUDE Irvine32.inc library link

  

this is an error → → programb.obj: error LNK2019: unresolved external symbol _scanf referenced by the function _main @ 0

  

TURN ON 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

.... , , ....

+3
4

-S gcc : gcc myfile.c -S -o myfile.s

, .

+9

, . NASM, MASM. , EBX . .: -)

section .data
answer: dd 0
right:  dd 0
wrong:  dd 0

section .text
extern printf
extern scanf
global main
main:

   push ebx
   mov ebx, 1

.loop_start:

   cmp ebx, 11
   je .loop_end

   push 1
   push ebx
   push .string0
   call printf
   add esp, 12

   push answer
   push .string1
   call scanf
   add esp, 8

   inc ebx
   cmp dword [answer], ebx
   jne .wrong

   push ebx
   push .string2
   call printf
   add esp, 8
   inc dword [right]

   jmp .loop_start

.wrong:

   push ebx
   push .string3
   call printf
   add esp, 8
   inc dword [wrong]

   jmp .loop_start

.loop_end:

   push dword [wrong]
   push dword [right]
   push .string4
   call printf
   add esp, 12

   pop ebx
   xor eax, eax
   ret

section .data

.string0:
   db "What is %d + %d?  ", 0
.string1:
   db "%d", 0
.string2:
   db "right!  ", 0
.string3:
   db "Sorry, you're wrong.   The answer is %d", 10, 0
.string4:
   db "You got %d right and %d wrong.", 10, 0
+5

, printf , . , , , . printf. , - . , , :

       call     printf

, printf , , ( ) - , . , printf , , + 1. , printf , .

, !

PS Re: previous answers, it is usually not useful to look at the assembly code generated by the compiler if you are trying to learn the assembly. Even without optimization enabled, the output is not intended for people to read.

+1
source

As an example, this line in the C programming language:

printf("\n%d%s%d\n\n",num1," is not equal to ",num2);

is equivalent to:

printf PROTO arg1:Ptr Byte, printlist:VARARG
.data
msg1fmt byte 0Ah,"%d%s%d",0Ah,0Ah,0
msg1 byte " is not equal to ",0
num1 sdword 5
num2 sdword 7
.code
main proc
INVOKE printf, ADDR msg1fmt, num1, ADDR msg1, num2
ret
0
source

Source: https://habr.com/ru/post/1773198/


All Articles