Lab_Assignment__2_hhhgyf_lab2_hussain_hamadah_14054006

advertisement
Hussain Hamadah
ECE 2210-G. N. DeSouza
14054006
Lab description:
This program should add nine numbers which can be any typed like binary, hexadecimal, decimal numbers;
then saves them. The numbers are given as an array. And we are using the accumulators A, B to save the sum
and keep track of the number of items added. And X as an index.
Requirements: Use wookie simulator to run the program
The program must begin at $C000, The data array must begin at $0010.
Pseudocode:
Initialize num_counted, sum and index
LOOP
sum <- sum + DATA1[index]
if sum doesn't fit in 8-bits
then stop with an error
otherwise, just continue
index <- index + 1
num_counted <- num_counted + 1
if last element has been counted
then save summation and stop without error
otherwise, go back to LOOP
this program adds a sequence of 9 numbers stored in DATA1.
The numbers are represented using 8 bit unsigned integers.
The program must end with the correct summation if such a
value can too be represented using 8 bit unsigned integers
and it must abort with an error if an overflow occurs.
The input parameters are DATA1 and NUM_ELM, which provide
respectively the sequence of numbers and the number
of elements in the sequence.
The output parameters are SUM and ERROR, and they are used to
store the value of the summation and to indicate when an
error occurred, respectively.
Hussain Hamadah
ECE 2210-G. N. DeSouza
14054006
Flow chart:
Start
A=0
B=0
D=0
Get number and
save in D
B=B+1
A=A+D
Yes
A > 128
No
B < 10
No
Show A
End
Yes
Hussain Hamadah
ECE 2210-G. N. DeSouza
14054006
PART 1: the given numbers “The nine numbers are given in decimal: 3,-5,13,24,11,25,36,2,8”
1: Sum is $75 (75 in hex) at memory location $001a
S
0
X
1
H
1
I
1
N
0
Z
0
V
0
C
0
With this program the actual sum should not be longer than 8-bit long number. And the
numbers output if added in decimal is (117) which equal to $75. And that is the right sum.
There is no overflow.
Hussain Hamadah
ECE 2210-G. N. DeSouza
14054006
PART2: testing “The nine numbers are given in decimal: 10,15,77,45,-23,5,12,37,28”
2: since I am using 8 - bits memory , I can’t represent this number in just 8 bits , so there was
an overflow and the program stopped and did not show the sum as expected when the
overflow flag changes to 1 . And since I am using signed numbers the carry flag cannot be used
to represent an overflow.
Hussain Hamadah
ECE 2210-G. N. DeSouza
14054006
PART3 : another testing sample “The nine numbers are given in decimal: 1,2,3,4,5,6,7,8,9”
3: Sum is $2D (2D in hex) at memory location $001a
S
0
X
1
H
0
I
1
N
0
Z
0
V
0
C
0
As we can see that the sum is less than 128 which is no problem for this program to add since
there is no overflow.
Hussain Hamadah
ECE 2210-G. N. DeSouza
14054006
*************************************************************************
* Name: Hussain Hamadah
* Student #: 14054006
* Lab 1: Summation of 9 8-bit long numbers.
* Date: 3/6/08
*
* Description:
*
This program adds a sequence of 9 numbers stored in DATA1.
*
The numbers are represented using 8 bit unsigned integers.
*
The program must end with the correct summation if such a
*
value can too be represented using 8 bit unsigned integers
*
and it must abort with an error if an overflow occurs.
*
The input parameters are DATA1 and NUM_ELM, which provide
*
respectively the sequence of numbers and the number
*
of elements in the sequence.
*
The output parameters are SUM and ERROR, and they are used to
*
store the value of the summation and to indicate when an
*
error occurred, respectively.
*
* Pseudocode:
*
Initialize num_counted, sum and index
*
LOOP
*
sum <- sum + DATA1[index]
*
if sum doesn't fit in 8-bits
*
then stop with an error
*
otherwise, just continue
*
index <- index + 1
*
num_counted <- num_counted + 1
*
if last element has been counted
*
then save summation and stop without error
*
otherwise, go back to LOOP
*
*************************************************************************
***** Data Segment *****
ORG
$10
Data Segment to be stored at $1000
**** DATA1
FCB
%00000011,%11111011,%00001101,%00011000,%00001011,%00011001,%00100100,%00000010,%00001000
* you can use the decimal format, no need for conversion unless you have been told to do so
DATA1 FCB
3,-5,13,24,11,25,36,2,8
Sequence of numbers to be added
* NUM_ELM FCB
$9
Number of elements in DATA1
* when you define a constant, you use EQU and not RMB (Reserve Memory Byte!)
NUM_ELM EQU
9
Number of elements in DATA1
* ERROR FCB
0
Reserve 1 byte of memory to store the error code (0="no error", 1="error")
* SUM FCB
0
Reserve 1 byte of memory to store the summation
* when you want to reserve a byte use RMB (Reserve Memory Byte)
* FCB is used to declare and initialize a variable
* In other words, you can think about ERROR FCB 0 as doing int ERROR = 0 in C++ or Java
* All you want is reserve a byte to store some data in so use RMB and not FCB
Hussain Hamadah
ERROR RMB
SUM RMB
1
1
ECE 2210-G. N. DeSouza
14054006
Reserve 1 byte of memory to store the error code (0="no error", 1="error")
Reserve 1 byte of memory to store the summation
***** Code Segment *****
ORG
$C000
Code Segment to start at $C000
CLRA
Prepare accumulator A to store the number of elements counted
CLRB
Prepare accumulator B to store the sum
LDX
#DATA1
Prepare register X to index array DATA1
DO_SUM
ADDB
0,X
Add the next number in DATA1 and keep summation in accumulator B
* BCS
OVERFLOW
Branch to OVERFLOW if summation doesn't fit in 8 bits (that is, carry bit is set)
* are you dealing with unsigned or signed numbers?
* The c flag is set when the carry bit is set. That is, c flag is set when there is an overflow of adding UNSIGNED numbers
* What can I use to detect overflow with SIGNED numbers?
* Use the v flag which is set when there is an overflow resulting from adding SIGNED numbers.
BVS
INX
INCA
OVERFLOW
Branch to OVERFLOW if summation doesn't fit in 8 bits (that is, carry bit is set)
Increment index of the array DATA1
Increment the number of elements counted so far
CMPA
#NUM_ELM
Check if this was the last element in the sequence
* use immediate addressing mode to access a constant defined using EQU
BLO
STAB
BRA
DO_SUM
SUM
END
Keep doing the summation if 9 < NUM_ELM
Otherwise, store summation in SUM
Branch to the end
OVERFLOW
LDAA
#1
Set error flag
STAA
ERROR
END
SWI
Stop
END
Conclusion:
The program deals with negative numbers easily. The steps in the program can vary but all
should give the same output.
Download