Spot the problem
Posted: November 11, 2015 Filed under: Bug, Error, Loop control, Sum, Y86 | Tags: irmovl, mrmovl Leave a commentThis is a classic beginner problem in Y86 classes:
Write a program that creates a sum of all inclusive integers between X and Y, with the sum, SUM, and X and Y being global variables.
Solving this problem requires a loop, and the point of the problem is to learn loop management.
The problem is fairly trivial, but things can go wrong. Below is an example of how it went wrong for one student. Can you find the problem? If not, scroll past the program to see the answer.
.pos 0 init: irmovl Stack, %esp // Set up stack pointer irmovl Stack, %ebp // Set up base pointer call main // Call main program halt // Terminate program main: pushl %ebp // Setup rrmovl %esp, %ebp pushl %ebx // Declare x local register irmovl x, %ebx pushl %esi // Declare y local register irmovl y, %esi pushl %eax // Declare sum irmovl sum, %eax pushl %edi // Set a constant 1 irmovl $1, %edi // ...to get the loop started L2: subl %ebx, %esi // %esi = y-x jl L3 // Ends function if x greater than y irmovl y, %esi // %esi = y addl %ebx, %eax // sum += x addl %edi, %ebx // x++ jmp L2 // Go to beginning of loop rmmovl %eax, (%eax) // Assign value to sum L3: rrmovl %ebp, %esp // Finished. Perform house cleaning popl %ebx popl %esi popl %edi popl %eax popl %ebp ret // Back where we belong! .align 4 // Get on the boundary x: .long 1 // Set X to 1 y: .long 4 // Set Y to 4 sum: .long 0 .pos 0x200 Stack: .long 0
Do you see the problem? No? Here is a clue. Focus on IRMOVL. See it?
Ok, here it is… the program assigns the addresses of X and Y to registers instead of the appropriate values.
Let’s run the program and stop after the assignment of X.
As you see (if you click on the image to zoom in,) the EBX register has been assigned x’60 as a value instead of 1. And guess what? x’60 is precisely the location of X.
Let’s run the program again, this time stopping after the assignment of Y.
As you see, the ESI register has been assigned x’64 as a value instead of 4. And guess what? x’64 is precisely the location of Y.
Know how to fix it? No? Try using MRMOVL instead.
Now look at the assignment of SUM. Anything you would want to change here?
This is a classic mistake. The clue could be found in the X and Y values.