When designing an assembly language, a language to manipulate a CPU, there are 3 major concerns:
- Transferring data from main memory to memory internal to the CPU (registers or operand stack).
- The set of operations that can be performed by the ALU on the data, for example
- A way to provide program control, for example to implement branch (if) and looping (for or while) type structures in a program. Normally control structure will be provided by a branch operation.
These three major concerns, and how they are addressed in the assembly language, will be discussed in the next sections. The last section of this chapter will give some programs that will illustrate how a program will be written in this assembly language.
2.5.1 Transferring data from main memory to internal CPU memory
The amount of memory directly accessible to a programmer on the CPU (e.g. registers) is very limited. In the case of the one-address architecture, only one memory slot, the
$ac, is directly useable by a programmer. Therefore programs need to rely on main memory to store program instructions and data.
To transfer items from data memory to the $ac the instructions add, sub, and stor are used.
sub instructions, the second operand of the instruction is the label or memory address of the value to retrieved from memory and sent to the ALU. So for example, to load a value into the $ac from a memory location labelled A the following code would be used.
clac add A
Note that the
$ac should always be set to 0 (using the
clac) before loading a value into the $ac, or the value stored in the
$ac will be the result of adding the value at memory location A with the current value in the
stor instruction, the second operand is the label or memory address at which to
stor the value from the
$ac. For example, to store the value in the
$ac to memory at the address in label B, the following code would be used.
2.5.2 Set of valid ALU operations
The next consideration is the set of operations which the ALU can perform on the input data. This list depends on the complexity of the ALU. The ALU in this computer is very simple, and so will only support the operations add and sub.
2.5.3 Program Control (Branching)
To do any useful program, if and loop constructs must be supported. In the implemented one- address CPU this is accomplished by the Branch-if-equal-zero (
beqz) operation. For this operation, if the
$ac is 0, the program will branch to the text memory address that is contained in the branch statement. This address can be either a label representing the address, or the numeric value of the branch address. So in the following instruction, the program will branch to the address of label EndLoop if the value in the
An unconditional branch statement is often used, but can be simulated by first setting the
0 before the branch statement. The following instruction implements an unconditional branch.
clac beqz StartLoop
2.5.4 Assembler Instructions
Based on the criteria of the preceding section, A minimum set of assembler instructions is defined to create useful programs. These instructions are sufficient to create useful programs, and several examples will be shown at the end of this chapter.
- Either a label or the actual address of the value can be used in this instruction. Thus if the label A refers to the dm address of 5, the following two instructions are the same.
add A add 5
immediate- Add the immediate value in this instruction to the
$ac. The immediate value is an 8-bit integer value between -128...127
$ac <- $ac + immediate
An example of an addi instruction which adds 15 to the value in the $ac follows.
beqz [label/address]– The
beqzinstruction changes the value in the Program Counter (
$pc) to the text memory address in the instruction if the value in the
0. In this CPU, the
$pcalways specifies the next instruction to execute, so this has the effect of changing the next instruction to execute to the address in the instruction. This is called a program branch, or simply branch.
$pc <- address IF $ac is 0
An example of the
beqzinstruction that branches to address 16 if the
clacinstruction sets the
0. This could be done with a set of stor and sub operations, so instruction is mostly for convenience.
$ac <- 0
sub [label/address]- Subtract a value from data memory to the current
$ac <- $ac - dm[address]
Either a label or the actual address of the value can be used in this instruction. Thus if the label A refers to the dm address of 5, the following two instructions are the same.
sub A sub 5
subi immediate- Sub the immediate value in this instruction to the
$ac. The immediate value is an 8-bit integer value between -128...128.
$ac <- $ac - immediate
An example of an
subiinstruction which adds 15 to the value in the
stor [label/address]– Store the current value in the $ac to data memory.
dm[address] <- $ac
Either a label or the actual address of the value can be used in this instruction. Thus if the label A refers to the dm address of
5, the following two instructions are the same.
stor A stor 5
noop– this statement does nothing. Executing this statement does not change the value of any memory or registers (except the
$pc) in the system. It is included so that text memory can be set to 0 and executed without changing the internal state of the computer.