2

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.

Jens Björnhager
  • 5,632
  • 3
  • 27
  • 47
user1261445
  • 291
  • 1
  • 6
  • 15
  • 3
    Most C compilers have a switch that generates assembly for you: gcc -S, cl /Fa, etc... at least "x86" assembler for you ;) – paulsm4 Jul 10 '12 at 22:25
  • Just out of curiosity, how did compile this? y86 is a hypothetical language for homework problems – JoeCortopassi Jul 10 '12 at 22:27
  • In the assignment we compile using yas progName.c and then yis progName.yo – user1261445 Jul 10 '12 at 22:33
  • 1
    @JoeCortopassi - I believe the assembler ("yas") and simulator ("yis") come with this book: [Computer Systems: A Programmer's Perspective](http://csapp.cs.cmu.edu/) – paulsm4 Jul 10 '12 at 22:34

2 Answers2

4

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.

Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180
  • If I can;t do something like irmov1 %eax, %efx, then how would I set my efx equal to eax (given that I will now give efx a valid name)...this was the only way I knew how to do this given the notes I have. – user1261445 Jul 10 '12 at 22:47
  • There's no register named `efx`. You can only choose from existing registers. Correct the spelling of the instruction. It ends in "L", not in "one". – Alexey Frunze Jul 10 '12 at 22:52
  • And you need `rrmovl` to move from a register to a register. Read the y86 docs, there's enough info at the links I gave. – Alexey Frunze Jul 11 '12 at 23:58
0

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.

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
pajacobsen
  • 44
  • 2
  • Your link is not related to the topic of this question, and you've failed to indicate it's your own blog. – Andrew Barber Nov 08 '12 at 04:11
  • Not relevant? Well, of course the link it relevant. Clearly Jens, the poster, does not understand Y86 at all, and so the link leads him to a location where he can gain understanding. – pajacobsen Nov 08 '12 at 14:22
  • With respect to it being my blog, yes, I stand somewhat corrected. Actually it is *our* blog, belonging to a couple of guys who enjoy Y86. Regardless, I was not aware of the need for disclosure, and I stand corrected. – pajacobsen Nov 08 '12 at 14:23