Example Solution: Instruction Set Design

Part 1: Requirements

Design a simple assembly language instruction set that satisfies the following requirements:

  1. Every machine instructions is 16 bits long, and must have a unique opcode.
  2. All arithmetic and conditional jumps are unsigned.
  3. Code offsets range from 0000h to 0FFFh.
  4. Memory offsets range from 0000h to 0FFFh.
  5. Immediate operands can be decimal or hexadecimal. Hexadecimal constants begin with a "0x" prefix and do not have a trailing "h".
  6. The register set consists of sixteen 16-bit general-purpose registers, named R1 through R16. Register R1 is also the accumulator register.The source operand is never modified by an instruction.
  7. The destination operand is usually modified by an instruction.
  8. The address of a memory operand can be loaded into the accumulator.
  9. A source register can be moved to, added to, or subtracted from a destination register.
  10. A source register can divide a destination register.
  11. A destination register can be multiplied by a source register.
  12. A destination register can be ANDed, ORed, or XORed by a source register.
  13. A destination register's bits can be inverted by a NOT operation.
  14. Data can be loaded from a memory operand into the accumulator (register R1).
  15. The bits in a destination register can be shifted to the right n times, where n is stored in the source register.
  16. The bits in a destination register can be shifted to the left n times, where n is stored in the source register.
  17. Data can be stored from the accumulator (register R1) into a memory operand.
  18. A destination register can be compared to a source register by performing an implied subtraction of the source from the destination, affecting the Carry and Zero flags. (The destination register is not modified.)
  19. Immediate values (constants) can be moved to , added to, and subtracted from registers.
  20. An unconditional jump may be made to a code label.
  21. Jumps can be made based on the following conditions: Z, NZ, E, NE, B, A, AE, BE, C, NC.
  22. The ALU has Zero and Carry flags.
  23. The following memory operand types exist:
    1. address of memory operand: just the variable name. For example: array
    2. direct memory operand: variable name surrounded by brackets. For example: [array]
    3. indirect memory operand: register name surrounded by parentheses. For example: (R2)

Part 2: Documentation

Instruction Syntax
Sample
Description
ADD reg-d, reg-s
add R2,R3
Add register-s to register-d
ADI reg-d, imm8
adi R4,0x25
Add 8-bit immediate value to register-d
AND reg-d, reg-s
and R4,R2
Perform bitwise AND of all bits in register-s on all bits in register-d
CMP reg-d, reg-s
cmp R1,R2
Perform an implied subtraction of register-s from register-d, affecting the Zero and Carry flags
DIV reg-d, reg-s
div R6,R4
Divide register-d by register-s
Jcond label
cmp R1,R2
jne L2
Execute a conditional jump to a label. Possible values for cond are: Z, NZ, E, NE, B, A, AE, BE, C, and NC. The maximum range for the jump is -128 bytes to +127 bytes from the location of the next instruction.
JMP label
jmp L2
Unconditional jump to a code label
LDA mem
.data
myWord WORD ?
.code

lda myWord
Load address of memory operand into accumulator
LOD mem
.data
myWord WORD ?
.code
lod myWord
Load contents of memory operand into accumulator
MOV reg-d, reg-s
mov R2,R1
Copy contents of register-s to register-d
MUL reg-d, reg-s
mul R1,R2
Multiply register-d by register-s
MVI reg,imm8
mvi R2,0x25
Move immediate value to register
NOT reg
not R5
Invert all the bits in a register (bitwise NOT)
OR reg-d, reg-s
or R4,R2
Perform bitwise OR of all bits in register-s on all bits in register-d
SBI reg-d, imm8
sbi R4,36
Subtract 8-bit immediate value from register-d
SHL reg-d, reg-s
shl R4,R2
Logically shift register-d left by n bits, where register-s contains n
SHR reg-d, reg-s
shr R4,R7
Logically shift register-d right by n bits, where register-s contains n
STO mem
sto myWord
Store accumulator to memory
SUB reg-d, reg-s
sub R5,R2
Subtract register-s from register-d
XOR reg-d, reg-s
xor R4,R2
Perform bitwise exclusive-OR of all bits in register-s on all bits in register-d
 
 
 

 

Part 3: Test Programs

Program #1

Write a program using your new instruction set that calculates the sum of an array of fifty 16-bit integers and stores the sum in a variable. Use the following declarations, which we assume that your assembler recognizes:

COUNT = 50
.data
array WORD COUNT DUP(?)
sum   WORD ?        ; holds the sum
.code
	lda array       ; load address of array into accum
	mov R4,R1       ; store array index in R4
	mvi R5,COUNT    ; get loop counter
	mvi R6,0        ; R6 holds the sum

L1:	lod (R4)        ; load word indexed by R4 into accum
	add R6,R4       ; add word to sum
	adi R4,2        ; point to next array element
	sbi R5,1        ; decrement loop counter
	jnz L1          ; repeat loop if counter != 0

	mov R1,R6       ; move sum to accum
	sto sum         ; store accum to sum

Program #2

Write a program using your new instruction set that compares the contents of three variables named var1, var2, and var3, and moves the largest variabled to the accumulator (R1).

	lod var3
	mov R5,R1          ; R5 = var3
	lod var2
	mov R4,R1          ; R4 = var2
	lod var1           ; accum = var1
	cmp R4,R1          ; var2 > accum?
	jbe L1             ; no: skip
	mov R1,R4          ; yes: accum = R4

L1:	cmp R5,R1          ; var3 > accum?
	jbe L2             ; no: skip
	mov R1,R5          ; yes: accum = R5

L2:	                   ; accum holds largest value

Program #3

Write a program using your new instruction set that separates the hours, minutes, and seconds fields of a time stamp from a disk directory entry and moves the minutes field into a separate variable. The time format is shown on page 507.

.data 
timeStamp WORD ?		; starting value
minutes   WORD ?
.code
	lod timeStamp          ; accum = timeStamp
	mvi R3,5               ; set up shift count
	shl R1,R3              ; shift accum
	mvi R3,0x003F          ; bit mask for minutes
	and R1,R3              ; clear unused bits
	sto minutes            ; store in minutes

Program #4

Write a program using your new instruction set that implements the following expression:

	R6 = (var1 + var2) * var3

Do not permit var1, var2, or var3 to be modified. You can assume the following data definitions:

.data
var1 WORD ?
var2 WORD ?
var3 WORD ?
.code
	lod var2              ; accum = var2
	mov R2,R1             ; R2 = var2
	lod var1              ; accum = var1
	add R1,R2             ; accum = left side
	mov R6,R1             ; R6 = expression in parentheses
	lod var3              ; accum = var3
	mul R6,R1             ; R6 = expression value