简体   繁体   中英

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.

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 .

Jens,

First, Y86 does not have any efx, egx, and ehx registers, which is why you are getting the invalid lines when you pour the code through YAS.

Second, you make conditional branches by subtracting two registers using the subl instruction and jumping on the condition code set by the Y86 ALU by ways of the jxx instructions.

Check my blog at http://y86tutoring.wordpress.com for details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM