Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Assume that the variables a, b, c, d, e, and f are stored in consecutive memo

ID: 3918104 • Letter: 1

Question

1. Assume that the variables a, b, c, d, e, and f are stored in consecutive memory locations, and that a register, say $20, has the address for variable a.

(a) For the C language expression f = a + b + c + d;, what is the corresponding

EMU8086 assembly code?

(b) Do the same for the C language expression e = a + (f - 7);

2. Write equivalent C code for the following EMU8086 assembler fragments:

(a) add f, g, h ; (f ß g+h)

(b) addi h, i, 1 ;(hßi+1)   sub f, g, h ;(fßg-h)

3. Assume that variables f, g, h, i, and j are already stored in general purpose registers, say $s0 to $s4 respectively, and the addresses of the first element of arrays A and B are stored in index registers, say $s6 and $s7 respectively. Provide EMU8086 code.

(a) f=g+h+B[4]; (b) f = g - A[B[4]];

4. For the following, assume that variables a, b, and i are kept in general purpose registers and that a base register holds the base address of array D.

Write EMU8086 assembly code for the following C code fragments, try to minimize the number of assembly instructions:

(a) A simple for loop: for (i = 0; i < 10, i++) a += b;

(b) do the same thing for the following C code.

for (i = 0; i < 10, i+=2) {

D[i] = D[i] + 1;   D[i+1] = D[i+1] + 1;

                  }

(c) A while loop with an array:

while (a < 10) {

           D[a] = a + b; a += 1; // could also be a++; }

(d) An example of recursion:

int fib (int a, int b, int i) {

if (i == 0) return b; else return fib(a+b, a, i-1);

       }  

Explanation / Answer

Answer 1 -

(a)

C Expression:

f = a+b+c+d;

Corresponding EMU8086 assembly code

add g, c, d;

add h, g, b;

add f, h, a;

(b)

C expression:

e = a + (f-7);

Corresponding EMU8086 assembly code

addi b, f, -7;

add e, a, b;

Answer 2 -

(a)

The assembly fragment

add f, g, h;

add shows adding the values in two registers and then stored inthe third register.

denotes that g and h are to be added and then stored in f, in Cthe code will look like

f = g + h;

(b)

The assembly fragement

addi h, i, 1;

sub f, g, h;

addi: demonstrates adding a immediate value to the register andthen store back

sub: shows subtracting the values in two registers and store inthird

in C code will look like,

f = g-(i+1);

That is

h = i+1;

f = g - h;

Answer 3 -

(a)

f = g + h + B[4] ;

lw $t0, 16($s7) # loads 4 elements into Array B and stores it in $t0 that is $t0 = B[4]

add $t1, $s1, $s2 # adds g to h and stores itin $t1 that is $t1 = g + h

add $s0, $t0, $t1 # adds $t0, to $t1 and storesit in f that is f = g + h + B[4]

(b)

f = g - A[B[4]] ;

lw $t0, 16($s7) #$t0 = B[4]

sll $t0, $t0, 2 #$t0 = $t0*4= B[4]*4

add $t0, $t0, $s6 #$t0 = A[$t0] =A[B[4]]

lw $t0, 0($t0) #$t0 = A[B[4]]

sub $s0, $s1, $t0 #f = g - =g-A[B[4]]

Answer 4

(a)

Loop

(c)

Array

(d)

Recursion

Please hit thumbs up if you found this answer helpful.