You are on page 1of 11

Computer Programming TA C162

Topics to discuss…

Revisit Branch Instructions


Assembler Directives
Assembly Programs Examples

1
Computer Programming TA C162

BR (PC-Relative)

N -- negative
Z -- zero
P -- positive

Set by any instruction that writes a value to a register


(ADD, AND, NOT, LD, LDR, LDI, LEA)

Exactly one will be set at all times


Based on the last instruction that altered a register

2
Computer Programming TA C162

BR (PC-Relative): Data Path


Else PC contains the next sequential address (Branch Not Taken)

If the condition code used in BR is set to 1 the new PC contents will


be the addition of signed offset (IR[8:0]) to current PC value (Branch
is Taken)
What happens if bits [11:9] are all zero? All one?
3
Computer Programming TA C162

JMP (Register)
Jump is an unconditional branch -- Always taken.
• Target address is the contents of a register.
• Allows any target address.

4
Computer Programming TA C162

Recall the Adding Two Numbers


X3000 LD R0, #5 .ORIG x3000
X3001 LD R1, #5 LD R0, NUM1
X3002 ADD R2, R1, R0 LD R1, NUM2
X3003 ST R2, #4
X3004
ADD R2, R1, R0
X3005
ST R2, SUM
X3006 0000000000000100
HALT
X3007 0000000000000101
X3008 0000000000001001 NUM1 .FILL x0004
(Result)
NUM2 .FILL x0005
x3009
SUM .BLKW 1
.END

5
Computer Programming TA C162

LC-3 Assembly Language Syntax


Each line of a program is one of the following:
• An instruction
• An assembler directive (or pseudo-op)
• A comment
Whitespace (between symbols) and case are ignored.
Comments (beginning with “;”) are also ignored.
An instruction has the following format:
LABEL OPCODE OPERANDS ; COMMENTS

optional mandatory
6
Computer Programming TA C162

Opcodes and Operands


Opcodes
• Reserved symbols that correspond to LC-3 instructions
ex: ADD, AND, LD, LDR, …
Operands
• Registers -- Specified by Rn, where n is the register number
• Numbers -- Indicated by # (decimal) or x (hex)
• Label -- Symbolic name of memory location separated by comma
• Number, order, and type correspond to instruction format
ex:
ADD R1,R1,R3
ADD R1,R1,#3
LD R6,NUMBER
BRz LOOP
7
Computer Programming TA C162

Labels and Comments


Label
• Placed at the beginning of the line
• Assigns a symbolic name to the address corresponding to line
ex:
LOOP ADD R1,R1,#-1
BRp LOOP

Comment
• Anything after a semicolon is a comment Ignored by assembler
• Used by humans to document/understand programs

8
Computer Programming TA C162

LC-3 Assembler
•Takes LC-3 program as input, translate it into the ISA of LC-3
•Pseudo-ops or assembler directive helps in the translating process

Opcode Operand Meaning


.ORIG address starting address of program
.END end of program
.BLKW n allocate n words of storage
.FILL n allocate one word, initialize with value n

.STRINGZ n-character allocate n+1 locations,


string initialize w/characters and null terminator

9
Computer Programming TA C162

Program to compare two number and store larger number in R1


.ORIG X3000 ; Starting location of the program
LD R2, NUM1
LD R3, NUM2
NOT R4, R3
ADD R4, R4, #1 ; R4 contains –R3
ADD R1, R4, R2 ; R1 contains R2-R3
BRz Done
BRn LrgR3
BRp LrgR2
LrgR3 ADD R1, R3, #0 ;If R3 is larger than R2
BRnzp Done
LrgR2 ADD R1, R2, #0 ;If R2 is larger than R3
Done TRAP 0x25 ; Halt the program
NUM1 .FILL x1234
NUM2 .FILL x0123
.END

10
Computer Programming TA C162

Program to multiply a number by the constant 6


; program for 5x6
.ORIG x3000
LD R1, SIX
LD R2, NUMBER
AND R3, R3, #0 ; Clear R3. It will
; contain the product
; The inner loop
;
AGAIN ADD R3, R3, R2
ADD R1, R1, #-1 ; R1 keeps track of
BRp AGAIN ; the iteration.
;
HALT
;
NUMBER .FILL x0005
SIX .FILL x0006
;
.END
11