Sample Homework Assignment #2


Overview

This is a sample assignment, which is exactly like all the practice problems posted in this module. Just in case you wanted more practice, here you go. Some of the numbers in there care large-ish, and a calculator may be useful. In exams/quizzes numbers will be small and a calculator is not needed.

Solutions

Solutions are available as a plain ASCII file


Exercise #1: Memory Layout

Consider the following declaration in C:

int A = 0b011111;
int B[2] = {-27, -1};
int C = -034;
char D[4] = {'a',0,'z',027};
unsigned char E[3] = {0x43, 0, 0xAA};
short F = 0x43AA;

Question #1 [18pts]

Show the contents of the memory bytes starting at address A, in hex, on a machine that uses Little Endian. Indicate labels as well. For instance, a (wrong) answer could look like:

A     B     C  ...
FF FF FA 02 03 06 ...

Remember that we assume the compiler does not perform padding.

Question #2:

For each of the 6 declared variables, indicate whether it would lead to a different in-memory byte order on a Big Endian machine.


Exercise #2: Memory and Registers

Consider the following .data segment:

L1       L2       L3       L4       L5
3A FF 01 02 FF 6B B2 AA 42 41 61 62 D3 D5 D7

and the following program fragment:

mov	eax, L3
mov	ebx, L5
mov	ecx, 0
mov	cl, [L2]
sub ebx, ecx
mov ebx, [ebx]
add eax, 2
mov ecx, [eax]
mov [L4], cx
mov ax, [L1]
add bx, ax
mov [L1], ebx

After the code finishes executing, what are the contents of the 15 memory bytes starting at address L1, on a machine using Little Endian?

Here is the way to show your work for each instruction:

For instance, a partial answer (for a different program) could look like:

L1       L2       L3       L4       L5
3A FF 01 02 FF 6B B2 AA 42 41 61 62 D3 D5 D7

mov	ebx, L1 	; ebx = address of the 1st byte

mov	ecx, 0		; ecx = 00 00 00 00

mov	cx, [L3]	; ecx = 00 00 AA B2

mov	[L2], ecx	; updates memory state:

L1       L2       L3       L4       L5
3A FF 01 B2 AA 00 00 AA 42 41 61 62 D3 D5 D7

mov	ebx, L4		; ebx = address of the byte with value 41

mov 	bx, 12		; ebx = ?? ?? 00 0C

...