Format of an LC-2 Assembly program
A LC-2 assembly program begins with the .ORIG pseudo-op. This pseudo-op tells the assembler where in memory to put the LC-2 program. The line .ORIG x3000 instructs the assembler that the first line of the program is to be placed in memory location x3000.
The general format of instructions within the body of an assembly program is shown here. It consists of four parts in the following order.
The last line of the assembly program must contain the .END pseudo-op. This pseudo-op tells the assembler where the program ends.LABEL OPCODE operands ; CommentsThe LABEL is used to give a symbolic name to the memory location so the memory location can be referred to explicitly from other parts of the program. LABELs are useful because they identify the target of a branch instruction and identify the location where data is to be loaded. This field is optional unless another part of the program refers to this memory location.The OPCODE field is mandatory. This field specifies the instruction to be performed. All the OPCODEs of the LC-2 are found in table A.1 of Appendix A.
The operands field is also mandatory. This field should be specified in conjunction with the OPCODE field. The operands field specifies the things the instruction is supposed to do it to. If an instruction does not require any operands, the operand field is empty.
Last, the Comments field is optional but helpful. This field contains messages to the reader of the program about the purpose of the line in the program.
In addition to the .ORIG and .END pseudo-ops, your program might contain other pseudo-ops. The other two that have been introduced in class are the .FILL and .BLKW pseudo-ops. These pseudo-ops are useful in allocating space that is generally used for storing and loading data.
The .FILL pseudo-op sets aside a location in memory initialized to a particular value. The line .FILL x3300 allocates a location in memory and sets the contents of that memory location with the value x3300.The .BLKW pseudo-op sets aside a block of memroy locations. The line .BLKW x50 allocates 80 (hex 50) consecutive memory locations.
Notational Conventions
Constants may be specified in your program using the character '#' or 'x'. If a number is prefixed with the character '#', the number is interpreted as a decimal. If a number is prefixed with the character 'x', the number is interpreted as a hexadecimal. If neither a '#' nor a 'x' is specified before a number, the default interpretation of that number is hexadecimal. A negative contant can be formed by putting a negative sign in front of the number but after the '#' or 'x' character. The following examples show how constants are specified.
.FILL x10 ; hex 10
.FILL #10 ; decimal 10
.FILL x-10 ; hex -10
.FILL #-10 ; decimal -10
You should always specify whether you are using decimal or hex notation.
An example LC-2 Assembly program
;; This program adds a sequence of numbers starting at memory
;; location specified by the label DATA. It adds the sequence
;; of numbers until it encounters the sentinel whose value is
;; zero. The sum of the numbers is stored in memory location
;; specified by the label RESULT..ORIG x3000 ; program is to be loaded starting
; at memory location x3000
AND R0, R0, x0 ; clear the result
LEA R1, DATA ; R1 is a pointer to the dataAGAIN LDR R2, R1, #0 ; load one of the data values
BRZ DONE ; branch if we encounter the sentinel
ADD R0, R0, R2 ; add the data value to result
ADD R1, R1, x1 ; increment the pointer to the next data
BRnzp AGAIN ;DONE ST R0, RESULT ; store the result
HALTDATA .FILL #100 ; decimal 100
.FILL #-50 ; decimal -50
.FILL x10 ; hex 10
.FILL x0 ; sentinel
RESULT .FILL x0 ; this is where the
.END ; the last line of our assembly program