Professional Documents
Culture Documents
COSC 243 - Instruction Set Examples Computer Architecture
COSC 243 - Instruction Set Examples Computer Architecture
COSC 243 - Instruction Set Examples Computer Architecture
Write the instructions to add two decimal numbers, 50 and 72, and save the
answer in register C.
MVI
MVI
ADD
A, 50
B, 72
B
MOV
C, A
HLT
Page 1
Example 2
Write the instructions to add two decimal numbers, 50 and 72, and save the
answer in a memory location, called result. Use the assembler to name a
constant, called cnst, for 50 and to use a memory location, called xyz, for 72.
org
cnst
xyz:
equ
db
result: ds
start:
mvi
lxi
a, cnst
h, xyz
add
lxi
h, result
mov
M, A
end
; Set cnst to 50
; Allocate a memory location for 72,
; name it, and set it to 72
; Allocate a memory location for result
hlt
start
; End of assembly
Page 2
Example 3
Write the instructions to add two decimal numbers, 50 and 72 and save the
answer in a memory location called, result. Use the assembler to name a
constant, called cnst, for 50 and to use a memory location, called xyz, for 72.
This time use LDA, ADI, STA.
org
cnst
xyz:
equ
db
result: ds
start:
lda
xyz
adi
cnst
sta
hlt
result
end
start
; Set cnst to 50
; Set a memory location for 72, name it, and
; set it to 72
; Set aside a memory location for result
; Load the accumulator with the value at
; memory location, xyz
; Add the value, 50, represented by cnst to
; the value in the accumulator. The sum is
; stored in the accumulator.
; Store the answer in memory location, result
; Halt the processor
; End of assembly
Page 3
Example 4
Write the instructions to continually read a value on an input port whose
address is 100. If the number represented by the data on the input port is
greater than 128, turn on a light attached to bit 7 of an output port whose
address is 101. If the number is less than 128, turn off the light. Otherwise
read the value again.
org
inport equ
outport equ
setpt
equ
100
101
128
start:
mvi
out
A, 00H
outport
loop:
in
cpi
inport
setpt
jz
jp
loop
on
jm
off
hlt
on:
off:
end
mvi
A, 80H
out
jmp
outport
loop
mvi
A, 00H
out
jmp
outport
loop
start
Example 5
Write the instructions to load 3 into the accumulator, decrement the
accumulator to zero, and halt.
org
start:
mvi
A, 3
loop:
dcr
cpi
A
00H
jnz
loop
hlt
start
; End of assembly
end
Page 5
Example 6
Write the instructions to read a value on an input port whose address is 100.
If the number represented by the data on the input port is 232, call a
subroutine to add 3 to the value and store it in register E.
org
inport
equ
100
start:
lxi
in
cpi
SP, 255
inport
232
cz
hlt
sbrt
sbrt:
adi
mov
ret
3
E, A
end
start
Page 6
Example 7
The following partial program stores some critical values in the accumulator,
B, C, D, and E registers. It then calls a subroutine that is to read four values
from memory locations called P, Q, R, and S. Sum the values. The result is
stored in the memory location T. The subroutine then returns. It is
important that after the subroutine returns, the values in the accululator, B,
C, D, and E registers have not changed as a side-effect of the subroutine.
You are to add the code required to make sure the registers in the main
program are saved and restored in the subroutine. The locations of this code
is indicated in the partial program.
org 0
P:
Q:
R:
S:
T:
start :
db 20
db 21
db 22
db 23
ds 1
lxi
mvi
mvi
mvi
mvi
mvi
call
hlt
SP, 255
A, 10
B, 11
C, 12
D, 13
E, 14
sbrt
Page 7
sbrt :
end start
push A
push B
push D
lda
mov
lda
mov
lda
mov
lda
P
B, A
Q
C, A
R
D, A
S
add
add
add
sta
B
C
D
T
pop D
pop B
pop A
ret
; End of assembly
Page 8