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:
- Access an array of data in memory
- Analyze data accesses and predict behavior
- Use the base+displacement and scaled indexed accessing modes
Exercises
- Login to hopper.slu.edu- remember you can login with:
ssh username@hopper.slu.edu
- Now, download a new program for today's class:
wget http://cs.slu.edu/~dferry/courses/csci2400/asm/addressing.s
- 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.
- 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.
- 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.
- 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.
- 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
- Draw an arrow or otherwise indicate which byte the address of
myarray
refers to. Ask the instructor if you're not sure.
- Put square brackets [] around the four bytes that are accesssed by the operand (%eax).
- Put curly brackets {} around the four bytes that are accessed by the operand 4(%eax).
- Put asterisks ** around the four bytes that are accessed by the operand 3(%eax).
- Write out the four bytes of 3(%eax) as a binary number and compute its value.
- 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?
- 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)
.
- 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.
- Email your program to the instructor when you are done.
One change from last time- include your answers to each question in your email
or in a text file and include them with your submission. These will count for
credit as a homework assignment.