U08809 Microprocessors

advertisement
Mark A. E. Elphinstone-Hoadley
04131216
U08809 Microprocessors
Assembly Language Programming Assignment 1
1. Write a short assembly program that illustrates the use of the direct
addressing mode, and the use of the MUL function
Flowchart
Start
Move 2
numbers into
number1 &
number2 slot
Multiply
number in ‘a’
register by
‘b’ register
Move
number1 &
number2 into
arithmetic
registers
Move
solution from
‘a’ into total
Finish
Code
number1 equ 70h
number2 equ 71h
total
equ 72h
;create simple names for memory slots
org 00h
;address of first instruction
mov
mov
mov
mov
mul
mov
nop
;passive program end
number1, #06h;put numerical value 6 in slot named number1
number2, #02h;and value 2 in 'number2'
a, number1
;move number1 into accumulator
b, number2
;and number2 into second arithmetic register
ab
;multiply a by b
total, a
;move solution into 'total' slot
end
1
Mark A. E. Elphinstone-Hoadley
04131216
2. Three numbers are stored in memory locations 70h, 71h, and 72h, which
should be given the names X, Y and Z. Write an assembly language program
that finds the smallest number and stores it in 73h.
Flowchart
Start
Move 2
numbers into
number1 &
number2 slot
X < Y?
No
Yes
Y<Z?
Y = Smallest
Yes
No
X<Z?
No
Z = Smallest
Yes
Finish
X = Smallest
Code
X equ 70h
Y equ 71h
Z equ 72h
smallest equ 73h
;set up named memory
org 00h
mov X, #0A1h
mov Y, #0A2h
mov Z, #0A3h
mov a, X
subb a, Y
jc X_is_smallest
mov a, Y
subb a, Z
jc Y_is_smallest
jnc Z_is_smallest
;input numbers
;put x number in accumulator
;take y from x (a)
;is a =< 0 (x=smallest)?
;put y number in accumulator
;take Z from y?
;
;then z is smallest!
2
Mark A. E. Elphinstone-Hoadley
X_is_smallest:
mov a, Z
subb a, X
jc Z_is_smallest
mov smallest, X
sjmp finish
Y_is_smallest:
mov smallest, Y
sjmp finish
Z_is_smallest:
mov smallest, Z
sjmp finish
finish:
nop
04131216
;put Z in 'a'
;subtract X
;is a =< 0 (Z=smallest)?
;no? move X into smallest 'variable'
;move Y into smallest variable
;move Z into smallest variable
;done (passive end)
end
3. Write an assembly language program that finds the mean of three numbers.
You can assume that the sum of the three numbers does not exceed FFh
Flowchart
Start
Move 3
numbers into
number1, 2
& 3 slots
Move value 3
(number of
slots) into ‘b’
register
Move ‘a’
register value
into ‘mean’
slot
Add each
number
together in
accumulator
Divide value
in ‘a’ register
by ‘b’
register
Move ‘b’
register value
into ‘remain’
slot
Finish
Code
number1
number2
number3
mean
remain
equ 70h
equ 71h
equ 72h
equ 73h
equ 74h
org 00h
mov number1, #0Bh
;named memory slots for inputs
;slot for mean number (solution)
;slot for remaining number
;address of first instruction
;move value into each number slot
3
Mark A. E. Elphinstone-Hoadley
mov
mov
mov
add
add
mov
div
mov
mov
number2, #0Ch
number3, #0Dh
a, number1
a, number2
a, number3
b, #03h
ab
mean, a
remain, b
04131216
;move number1 into accumulator
;add number2 and number3
;move value 3 into ‘b’ register
;div register 'a' by register 'b'
;move calculation from ‘a’ into 'mean'
;move remainder from 'b' register into
;'remain'
end
4. Use a looping technique to find the sum of 12 numbers stored in locations 60h
to 6Bh. All the numbers will be less than 18h.
For this particular question I decided to take the question into depth by creating a
loop that generates an increasing number in slots 60h to 6Bh, whilst at the same time
adding the sum of each slot together in the same looping sequence. This is done using
a carried value to increase the number in ‘odd’ ascending format (e.g. 1,3,5,7...). The
sum is then put in its own slot, for good measure, to easily show each slot in action in
Keil uVision.
Flowchart
Start
Move value
into first slot
and put in
register ‘0’
Decrease R1
value by 1
Increase slot
number for
register ‘0’
and add carry
Carry new
value and
add carry to
sum
No
Add same
value to sum
(‘a’ register)
and carry it
Increase slot
number for
register ‘0’
and add carry
Is R1 =
0?
Yes
Move
directory 0Bh
into register
1
Increase
number in
new slot by 2
Move sum in
‘a’ register to
allocated
‘sum’ slot
Finish
4
Mark A. E. Elphinstone-Hoadley
04131216
Code
carry equ 6Ch
sum
equ 6Dh
;to use as carrying number
;to use for final sum
org 00h
mov 60h, #01h
mov R0, #60h
mov R1, #0Bh
mov a, @R0
mov carry, @R0
loop:inc R0
mov @R0, carry
inc @R0
inc @R0
mov carry, @R0
add a, carry
djnz R1, loop
mov sum, a
nop
;put numerical value 1 into 1st slot (60h)
;move input dir into register 0 (R0)
;move directory 0B into R1 (loop 11 times)
;move value in R0 into accumulator for sum
;keep carried number to be used in loop
;increment R0 directory by 1
;put carried number in new slot
;increase number in new slot by 2
;move new number into carried slot
;add carried value to accumulator for sum
;return to loop until R1 directory = 0
;put sum in it's own box, for good measure
;passively end the program
end
5
Download