Search code examples
cassemblyy86

Translate C code to assembly code?


I have to translate this C code to assembly code:

 #include <stdio.h>

 int main(){

 int a, b,c;
 scanf("%d",&a);
 scanf("%d",&b);
 if (a == b){
   b++;
 } 
 if (a > b){
  c = a;
  a = b;
  b = c;    
 }
 printf("%d\n",b-a);
 return 0;
 }  

My code is below, and incomplete.

    rdint %eax        # reading a
    rdint %ebx        # reading b
    irmovl $1, %edi

    subl %eax,%ebx
    addl %ebx, %edi
    je Equal


    irmov1 %eax, %efx  #flagged as invalid line
    irmov1 %ebx, %egx
    irmov1 %ecx, %ehx
    irmovl $0, %eax
    irmovl $0, %ebx
    irmovl $0, %ecx

    addl %eax, %efx    #flagged as invalid line
    addl %ebx, %egx
    addl %ecx, %ehx



    halt

Basically I think it is mostly done, but I have commented next to two lines flagged as invalid when I try to run it, but I'm not sure why they are invalid. I'm also not sure how to do an if statment for a > b. I could use any suggestions from people who know about y86 assembly language.


Solution

  • From what I can find online (1, 2), the only supported registers are: eax, ecx, edx, ebx, esi, edi, esp, and ebp.

    You are requesting non-existent registers (efx and further).

    Also irmov is for moving an immediate operand (read: constant numerical value) into a register operand, whereas your irmov1 %eax, %efx has two register operands.

    Finally, in computer software there's a huge difference between the character representing digit "one" and the character representing letter "L". Mind your 1's and l's. I mean irmov1 vs irmovl.