3

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

2
  • So are you asking how to call printf from assembly, or are asking about how to do integer to string conversion and then string concatenation in assembly? These are two pretty different things. Nov 5, 2010 at 12:33
  • basically the string concatenation
    – jwill22
    Nov 5, 2010 at 20:28

4 Answers 4

9

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.

3
  • 2
    ... And append the flag -masm=intel to obtain an MASM-looking code (intel syntax): gcc -S -masm=intel myfile.c -o myfile.s
    – Noe
    Nov 5, 2010 at 8:18
  • Unfortunately this will generate code that delegates to the libc, instead of generating code to actually do the manipulation. Nov 5, 2010 at 8:24
  • 3
    Then he can compile in static mode, but recoding printf() will be a hell ! Nov 5, 2010 at 8:27
5

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. :-)

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
2
  • @user498084 I have not really used MASM, but it's supposed to be very similar. I'm sure you can figure it out. Also, the angry comment towards @Rob is not called for.
    – asveikau
    Nov 6, 2010 at 18:39
  • Ok I feel u....I tried shoots(sad face) I got it run but it didn't run like the c code. It's all good. And forget that he deserved it..if he didn't have anything constructive to say or contribute then he should of shut the hell up
    – jwill22
    Nov 7, 2010 at 17:38
1

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 of printf. 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:

       call     printf

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 then printf 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.

1
  • 2
    Well, obviously it's not meant for humans, but you can easily write a simple function that just calls printf() in a very simple case to see how it's called. Nov 5, 2010 at 8:31
0

As an example , this line in 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.