Solution 4.a Basic Int Arithmetic v.4

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Computer organisation and programming course, Shenkar College of Design & Engineering

Solution of Homework 4.a: unsigned & signed integer arithmetic

Figure 1 Single length (word) arithmetic - addition


1. Unsigned integer arithmetic: write a single word integer addition program using Mano’s CPU. Try
it with different numbers:
unsigned short int X = 65534;
unsigned short int Y = 1;
unsigned short int R;
R = X + Y;
LDA X
ADD Y
STA R // R = X + Y;
HLT
// Data
X, DEC 65534 // = FFFF hex
Y, DEC 1
R, DEC 0
Answer the following questions:
a) What is the range of values that should be assigned to X and Y?
0  +65,535 or in Hex: 0  FFFF
b) How is the result R interpreted?
All the bits of the n 16 bit integer represent a magnitude and should be taken into account.
Thus: FFFF = 65,535
c) How is overflow/underflow detected – or what indication do we get about it?
There is no underflow in addition of unsigned numbers. Overflow results from adding two
numbers whose sum exceeds the maximum possible of 65,535
Overflow in unsigned addition is detected by the End-carry becoming 1.

+ve + +ve may cause: Overflow overflow E←1


FFFE FFFF 0 0001 0002

65534 65535 0 +1 +2

+32768 +32767

8000 7FFF

© Dr Yigal Hoffner 1 Sunday, 03 April 2022


Computer organisation and programming course, Shenkar College of Design & Engineering

2. Signed integer arithmetic: write a single word integer addition program using Mano’s CPU. Try it
with different numbers (32767, -32768, etc.):
signed short int X = -1;
signed short int Y = 1;
signed short int R;
R = X + Y;
Answer the following questions:
a) What is the range of values that should be assigned to X and Y?
-32768  32,767 or in Hex: 8000  7FFF
b) How is the result R interpreted? With signed integer arithmetic we can have overflow as with
unsigned integers, but also underflow as the result of adding 2 negative numbers.
We use the most significant bit to determine whether the result is bigger than smaller than 0.
a. If Result msb = 0 - it is a positive number and its magnitude between:
0  +32,767 in Hex: 0 – 7FFF
b. If Result msb = 1 - it is a negative number. Use the 2's complement method to convert it
to its positive equivalent to determine its magnitude:
(-32,768  -1 in Hex: 8000 FFFF)

c) How is overflow/underflow detected – or what indication do we get when it takes place?


Does the End-carry help us when dealing with signed integers addition (as it does with unsigned
integer addition)? NO!
While with unsigned integer addition, the End-carry indicates overflow, with signed integer
addition, the End-carry gives no indication of overflow or underflow.
With signed integer addition, if we add 2 positive integers and the result is a negative integer –
we have Overflow.
With signed integer addition, if we add 2 negative integers and the result is a positive integer –
we have Overflow.
The test therefore revolves around checking the most significant bits of the two operands and the
result.
(Over or underflow is represented by V; V = 1 => over/underflow
V = 0 => NO overflow NO
signed int Operand1, Operand2, Result…
V=0;
Result = Operand1 + Operand2;
IF ( (Operand1)msb == (Operand2)msb )
THEN IF ( (Operand1)msb != (Result)msb )
THEN V=1;
IF ((Result)msb == 1)
THEN Error overflow! // V=1, S=1 (Sign)
ELSE Error underflow! // V=1, S=0 (Sign)
FI
FI
FI
-1 0 +1
-2
FFFF 0 0001
FFFE

-ve = negative +ve = positive


Underflow

8000 7FFF
-32768 +32767

Overflow
© Dr Yigal Hoffner 2 Sunday, 03 April 2022
Computer organisation and programming course, Shenkar College of Design & Engineering

3. Unsigned & Signed integer arithmetic: compare the results of doing addition in (2) & (3) – what is
happening? What is the difference between the two cases?
The result FFFF has to be interpreted in the right context!
The programmer determines the context when she decides whether we are dealing with signed or
unsigned integers. The result has to be interpreted accordingly.

LDA X
ADD Y
FFFE hex STA R
0001 hex
FFFF hex
Must be interpreted as Must be interpreted as

65,534 (FFFE) -2 (FFFE)


+ 1 + 1
unsigned integer 65,535 (FFFF) signed integer -1 (FFFF)

© Dr Yigal Hoffner 3 Sunday, 03 April 2022

You might also like