CSCI 2400: Assembly Class 2

Addressing Modes


We've said several times in class that a modern computer is just two things: a processor and a (large) contiguous array of bytes in memory. Assembly programming really makes it clear why this is the case: we have CPU operands and we have memory that we can reference. Understanding how memory is used illustrates exactly what is going on behind every major piece of software you use: compilers, operating systems, etc.

In today's class you will:

  1. Access an array of data in memory
  2. Analyze data accesses and predict behavior
  3. Use the base+displacement and scaled indexed accessing modes

Exercises

  1. Login to hopper.slu.edu- remember you can login with:

    ssh username@hopper.slu.edu

  2. Now, download a new program for today's class:

    wget http://cs.slu.edu/~dferry/courses/csci2400/asm/addressing.s

  3. Take a look at this program in a text editor. It's pretty close to what we had last time, but now we have declared an array named myarray. Print the first value of this array using our code from last time as a template.

  4. Now, use the leal instruction (load effective address) to load the starting address of myarray into register EAX. The first operand should be the name of the array, and the second operand should be the EAX register. Then, print out the same value as before, but be careful that now you need to dereference the EAX register (put parentheses around it). If you get a large and unexpected number you're printing the address itself, not the value pointed to.

  5. Still using the address of the array as computed by leal, modify your code to print out the second value of the array. Remember that a long value is 4 bytes wide.

  6. The first two values of the array are 100 and 200, respectively. Write down the 4-byte representation of both of these numbers in either hexadecimal or binary. Feel free to use an online calculator.

  7. Recall that memory is a contiguous array of bytes. The array we have declared also allocates memory contiguously. Write down the eight bytes from the previous exercise as they would appear in memory. However, this is not the order they would appear as you write them on paper next to each other- the least significant byte is stored in the first byte, the second most significant in the next byte, and so on.

    For example, for the number 500:

    Number 500 in binary:
    00000000 00000000 00000001 11110100
    byte 3   byte 2   byte 1   byte 0
    
    Number 500 in memory, with least significant byte first:
    11110100 00000001 00000000 00000000
    byte 0   byte 1   byte 2   byte 3
    

  8. Draw an arrow or otherwise indicate which byte the address of myarray refers to. Ask the instructor if you're not sure.

  9. Put square brackets [] around the four bytes that are accesssed by the operand (%eax).

  10. Put curly brackets {} around the four bytes that are accessed by the operand 4(%eax).

  11. Put asterisks ** around the four bytes that are accessed by the operand 3(%eax).

  12. Write out the four bytes of 3(%eax) as a binary number and compute its value.

  13. Print out the value of 3(%eax) using your program and check the value against your answer to the last exercise. What value does your program print?

  14. Up to this point we have been doing base + displacement addressing. For the last two exercises, we will shift to using the scaled indexed accessing mode. Re-write your program to print out the first element of the array using a base register, an index register, and a scale. To do so, first move the immediate value zero ($0) into the register EBX- this will serve as our index register. Since we are dealing with 4-byte data, our scale will be 4.

    Then, you can access the first element of the array with the operand (%eax, %ebx, 4).

  15. Lastly, modify your program to print out all elements of the array with the scaled indexed notation. First modify your string variable to include seven format specifiers. Then, push all seven values of the array onto the stack- incrementing the value of EBX between each. You can increment EBX with the instruction:

    incl %ebx

    You can print the values in any order you choose, but make sure you understand the ordering that you get.

  16. Save your program for submission later. One change from last time- include your answers to each question in a text file and include them with your submission. These will count for credit.