2

posted below is the code I have for a simple y86 assembly program. Given two integers, it should print out the larger of the two. To the right of each line I have an equivalent C translation.

# I ask about the need for a first line comment below.
rdint   %eax          # scanf("%d", &a);
rdint   %ebx          # scanf("%d", &b);
rrmovl  %eax, %ecx    # c = a;
subl    %ebx, %ecx    # c = a - b;
jge     ALarger       # if (c >= 0) { goto ALarger };
wrint   %ebx          # printf("%d", b);
jmp     End           # goto End;

ALarger:
wrint   %eax          # printf("%d", a);

End:
irmovl  $10, %ecx     # c = 10;
halt
wrch    %ecx

Using the assembler yas, the resulting .yo file looks like this:

0x000: f118         | # I ask about the need for a first line comment below.
0x002: f208         | rdint   %eax          # scanf("%d", &a);
0x004: f238         | rdint   %ebx          # scanf("%d", &b);
0x006: 2001         | rrmovl  %eax, %ecx    # c = a;
0x008: 6131         | subl    %ebx, %ecx    # c = a - b;
0x00a: 7514000000   | jge     ALarger       # if (c >= 0) { goto ALarger };
0x00f: f338         | wrint   %ebx          # printf("%d", b);
0x011: 7016000000   | jmp     End           # goto End;
                    |
0x016:              | ALarger:
0x016: f308         | wrint   %eax          # printf("%d", a);
                    |
0x018:              | End:
0x018: 30810a000000 | irmovl  $10, %ecx     # c = 10;
0x01e: 10           | halt
  • This has not been assembled right. I was told that wherever a label is encountered, it is replaced by the address of where it is found in the program. If the first number entered is larger, the instruction at line 0x00a is 7514000000. This is telling the program counter to go to line 0x014 (a line that doesn't even exist) when it should be telling it to go to 0x016. The same problem exists for line 0x011. Why is this happening?
  • When I assemble the program using the address lines instead of labels, the result is printed, however the newline is not. How can I fix this?
  • Finally, a minor question: If I did not have a comment as the first line, the first line of code is ignored. Is this supposed to happen?

Thank you for your time, I look forward to any answers you can provide.

Stout Joe
  • 324
  • 2
  • 4
  • 14
  • i don't know anything about this, but looking around on the net it seems like the other examples i can find have the assembly indented, with the labels to the left of the same line eg http://csapp.cs.cmu.edu/public/simguide.pdf - that might explain the label addresses being wrong and, if it was a really crappy implementation, might even explain the need for an initial comment... in other words, a wild guess is that your input format is wrong and the yas parser is confused. – andrew cooke Apr 11 '12 at 00:16
  • What happens if you put the label on the same line as the instruction: `ALarger: wrint %eax` on the same line, etc. – Jeff Apr 11 '12 at 00:44
  • When I do that the line for ALarger: wrint %eax is still 0x016 and End: irmovl $10, %ecx is still 0x018. The jge and jmp instructions also remain unchanged. Seems to be a stylistic thing. Thanks anyway @andrew. – Stout Joe Apr 11 '12 at 01:00

2 Answers2

3

YAS requires a new-line after the last line in order to build correct yo code. See http://y86tutoring.wordpress.com/2012/11/06/yas-no-halt-for-the-wicked/ for details.

pajacobsen
  • 44
  • 2
2

You put the halt statement before wrch, so execution stops before writing the newline, and it looks like yas just completely ignores it. This might explain your other problems, like the jumps being shifted by one byte; it's possible that having the number of lines in the program greater than those that are actually assembled has confused the assembler when it goes to put in the address jumps.

scry
  • 1,237
  • 12
  • 29
  • Sure enough, I tried it and if you switch those last two lines it works fine. – scry Apr 11 '12 at 02:32
  • Your logic is sound and that does make sense. I'm not surprised it worked for you, this is how it should be. I originally wrote this code the way you suggested, but when assembled, the .yo file goes haywire. The final line "halt" is not produced, every address is incremented by one, and when run the program stops in 1 step. It runs into Exception 'HLT', aka, halt. I have found that not putting the newline AFTER halt produces this problem for me. I have a feeling this might be a problem outside of my control and will contact my professor about it soon. Thank you for your response. – Stout Joe Apr 11 '12 at 06:36
  • Fiddled around with it and you were right! The trick was pressing 'Enter' immediately after halt. There needs to be an empty line after halt? y86 is strange. – Stout Joe Apr 11 '12 at 15:21
  • Yeah, that's a weird idiosyncrasy of y86 that I should have mentioned. I had to learn it the hard way too. – scry Apr 12 '12 at 02:44