Name: Spyros Blanas
Section: 1

Problem 1

Here is the assembly file that generated the code shown above.

Problem 2

Here is the assembly file that generated the code shown above.

Problem 3

Here is the assembly file that generated the code shown above.

Problem 4

  1. I would use the PC-Relative addressing mode because of its simplicity and because the distance falls within the limits of this mode.

  2. The Base+offset addressing mode has no restrictions on how far away the data are stored. I would first use operate-type instructions to load the appropriate value into a base register, and then load from that register (base) with an offset of 0.

  3. I would use the method described above for contiguously stored values. I would use the LEA (immediate addressing mode) instruction to make a register point at the start of the contiguous segment, and then use Base+offset to load specific values.

Problem 5

  1. ADD: operate
  2. JMP: control
  3. LEA: data movement
  4. NOT: operate

Extra Credit

The simplest way to compare two numbers is to subtract one from the other and see if the result is positive, negative or zero. But if the subtraction causes an overflow then the output may not be the correct one. For example when R3 = 0x8765 and R4 = 0x7654, R3 - R4 = -0x11111, which causes an overflow. If we discard the carry, the resulting number is positive, which would indicate that R3 is larger than R4, whereas it isn't.

When we add two numbers, overflow can occur only if they have the same sign. Similarly in the case of subtraction, overflow can occur only when they have different signs. If we are comparing a positive and a negative number then the postive one is always greater, without considering the magnitude of the numbers. We can use these facts to get around the overflow problem. The sample code explains how you can do it.

ADD R3, R3, #0
BRn R3ISNEGATIVE
;R3 is positive
ADD R4, R4, #0
BRp COMPARE
;R3 is postive, R4 is negative
BR COPYR3TOR1

R3ISNEGATIVE:
;R3 is negative
ADD R4, R4, #0
BRn COMPARE
;R3 is negative, R4 is positive
BR COPYR4TOR1

COMPARE:
;both of same sign, so compare
NOT R5, R4
ADD R5, R5, #1
ADD R5, R5, R3
BRn COPYR4TOR1

COPYR3TOR1:
ADD R1, R3, #0
BR  END

COPYR4TOR1:
ADD R1, R4, #0

END