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.