Reputation: 334
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
Thank you for your time, I look forward to any answers you can provide.
Upvotes: 2
Views: 2512
Reputation: 44
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.
Upvotes: 3
Reputation: 1257
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.
Upvotes: 2