COSC 243 - Instruction Set Examples Computer Architecture

You might also like

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

Example 1

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

COSC 243 Computer Architecture

; Load the number 50 into the accumulator


; Load the number 72 into register B
; Add the content of register B to the content
; of the accumulator. The sum is stored in the
; accumulator.
; Copy the sum from the accumulator into
; register C
; End of the program

Page 1

Instruction set examples

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

; The code is to be loaded into memory


; beginning at location 0
50
72

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

; Load 50 into the accumulator


; Load the address of xyz into the H-L
; register pair
; Add the value, 72, to the value in the
; accumulator. The sum is stored in the
; accumulator.
; Load the address of result into the H-L
; register pair
; Copy the accumulator to the memory
; location, result
; Halt the processor

start

; End of assembly

COSC 243 Computer Architecture

Page 2

Instruction set examples

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

; The code is to be loaded into memory


; beginning at location 0
50
72

result: ds

start:

lda

xyz

adi

cnst

sta
hlt

result

end

start

COSC 243 Computer Architecture

; 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

Instruction set examples

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

; The code is to be loaded into memory


; beginning at location 0

inport equ
outport equ
setpt
equ

100
101
128

; The input port is address 100


; The output port is address 101
; The set point is 128

start:

mvi
out

A, 00H
outport

; Turn off the light to start.

loop:

in
cpi

inport
setpt

jz
jp

loop
on

jm

off

; Read the input port into the accumulator


; Compare the value in the accumulator with
; the set point
; Continue the loop if the same
; Jump to on if the result is positive
; (input > 128)
; Jump to off if the result is negative
; (input < 128)
; Should never get here

hlt
on:

off:

end

mvi

A, 80H

out
jmp

outport
loop

mvi

A, 00H

out
jmp

outport
loop

start

COSC 243 Computer Architecture

; Make bit 7 of the accumulator a 1 to turn


; on the light
; Output the value to the output port
; Jump to loop to continue
; Make bit 7 of the accumulator a 0 to turn
; off the light
; Output the value to the output port
; Jump to loop to continue
; End of assembly
Page 4

Instruction set examples

Example 5
Write the instructions to load 3 into the accumulator, decrement the
accumulator to zero, and halt.
org

start:

mvi

A, 3

; Load the accumulator with 3

loop:

dcr
cpi

A
00H

jnz

loop

hlt

; Decrement the accumulator by 1


; Compare the value in the accumulator with
; zero
; Jump to on if the result is positive
; (acc > 0)
; Halt when the accumulator is zero

start

; End of assembly

end

; The code is to be loaded into memory


; beginning at location 0

COSC 243 Computer Architecture

Page 5

Instruction set examples

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

; The code is to be loaded into memory


; beginning at location 0

inport

equ

100

; The input port is address 100

start:

lxi
in
cpi

SP, 255
inport
232

cz
hlt

sbrt

; Set up the stack pointer


; Read the input port into the accumulator
; Compare the value in the accumulator with
; 232
; Call the subrouting if (value = 232)
; Stop the program

sbrt:

adi
mov
ret

3
E, A

end

start

COSC 243 Computer Architecture

; Add 3 to the value in the accumulator


; Copy the value to register E
; Return unconditionally to the program
; End of assembly

Page 6

Instruction set examples

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

; Load the code starting memory position 0

P:
Q:
R:
S:
T:

; Allocate memory locations for P, Q, R, S


; and initialise them to some values

start :

db 20
db 21
db 22
db 23
ds 1
lxi
mvi
mvi
mvi
mvi
mvi
call
hlt

; Allocate a memory location for T

SP, 255
A, 10
B, 11
C, 12
D, 13
E, 14
sbrt

COSC 243 Computer Architecture

; Load the stack pointer with 255


; Load A, B, C, D, E with critical values
; that we want to retain

; Call the subroutine


; Halt. Notice the original values were restored.

Page 7

Instruction set examples

sbrt :

end start

push A
push B
push D

; Store A and PSW on the stack


; Store B and C on the stack
; Store D and E on the stack

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

; Put the value at location P into accumulator


; Move it to B
; Put the value at location Q into accumulator
; Move it to C
; Put the value at location R into accumulator
; Move it to D
; Put the value at location S into accumulator
; Notice our registers are different to what they
; were when we started the subroutine.
; Add the four values
; Store the result in T

pop D
pop B
pop A

; Restore D and E to the original values


; Restore B and C to the original values
; Restore A and PSW to the original values

ret

; Return to calling program

; End of assembly

COSC 243 Computer Architecture

Page 8

Instruction set examples

You might also like