Tutorial 3
Tutorial 4
LC-3 instruction (3) & Assignment 1 Assistance
LC-3 instruction
Operate instructions: ADD, AND, NOT
Data movement instructions: LD, LDI, ST, STI, STR, LDR, LEA
Control instructions: BR, JMP, RTI, TRAP, JSR/JSRR
Unconditional branch – Trap
Assembler format
Trap trapvector8
Machine code format
Save current PC (after fetch) to R7
R7 <= PC
Set program counter to the content of trapvector8
PC <= mem[ZEXT(trapvector8)]
Opcode Redundancy Trapvector8
[15:12] [11:8] [7:0]
1111 0000 XXXXXXXX
Zero extension
Serves as index into table of service routine address
(0x0000-0x00FF)
Trap codes
Trap instruction is usually written in trap code in assembly language.
By using the trap code, you do not need to remember the offset address for each function.
Code Equivalent Meaning
HALT TRAP x25 Halt execution and print message to console
IN TRAP x23 Print prompt on console, read (and echo) one character from keyboard.
Character stored in R0[7:0]
OUT TRAP x21 Write one character (in R0[7:0]) to console
GETC TRAP x20 Read one character from keyboard.
Character stored in R0[7:0]
PUTS TRAP x22 Write null-terminated string to console. Address of string is in R0.
Unconditional branch - JSR
Assembler format
PC-relative mode: JSR Pcoffset11
Machine code format
Save current PC (after fetch) to R7
R7 <= PC
Set program counter to PC + SEXT(PCoffset11)
PC <= PC + SEXT(PCoffset11)
Opcode Mode Pcoffset11
[15:12] [11] [10:0]
0100 1 XXXXXXXXXXX
Unconditional branch - JSRR
Assembler format
Register mode: JSRR Base
Machine code format
Save current PC (after fetch) to R7
R7 <= PC
Set program counter to the content of base
PC <= Base
Opcode Mode Redundancy Base Redundancy
[15:12] [11] [10:9] [8:6] [5:0]
0100 0 00 XXX 000000
Assembler Directives
Are not executed by the program, used by the assembler
Exercise 1
What is the output after this program is executed?
Program:
.ORIG x3000
LEA R1, hello
loop LDR R0, R1, #0
BRz finish
OUT
ADD R1, R1, #1
BRnzp loop
finish HALT
hello .STRINGZ "Hello"
.END
Solution 1
LEA R1, hello
PC (before fetch): x3000
PC (after fetch): x3001
The address of label “hello” is x3007
DR = PC + SEXT(PCoffset9)
PC (after fetch) + SEXT(#6) = x3001 + x0006 = x3007
R1 = x3007
Now R1 stored the address of first char of the string “Hello”
R0 x0000
R1 x3007
R2 x0000
R3 x0000
R4 x0000
R5 x0000
R0 x0000
R1 x0000
R2 x0000
R3 x0000
R4 x0000
R5 x0000
Address Hex ASCII
x3007 x0048 “H”
x3008 x0065 “e”
x3009 x006C “l”
x300A x006C “l”
x300B x006F “o”
x300C x0000 “NULL”
Solution 1
loop LDR R0, R1, #0
“loop” is the label (ignore it at this moment)
Base + Offset Mode: LDR DR, Base, offset6
DR = mem[Base + SEXT(offset6)]
Base = R1 = x3007
Base + SEXT(offset6) = x3007 + x0000 = x3007
R0 = mem[x3007] = x0048
This instruction load the string char “H” into R0.
Address Hex ASCII
x3007 x0048 “H”
x3008 x0065 “e”
x3009 x006C “l”
x300A x006C “l”
x300B x006F “o”
x300C x0000 “NULL”
R0 x0048
R1 x3007
R2 x0000
R3 x0000
R4 x0000
R5 x0000
R0 x0000
R1 x3007
R2 x0000
R3 x0000
R4 x0000
R5 x0000
Solution 1
BRz finish
Branch to label “finish” if condition code register Z is set.
This instruction will branch to “finish” if R0 is loaded with x0000
(x0000 in ASCII is NULL char. Meaning end of string.)
Otherwise, the branch will NOT be taken.
Address Hex ASCII
x3007 x0048 “H”
x3008 x0065 “e”
x3009 x006C “l”
x300A x006C “l”
x300B x006F “o”
x300C x0000 “NULL”
R0 x0048
R1 x3007
R2 x0000
R3 x0000
R4 x0000
R5 x0000
Solution 1
OUT
Equivalent to Trap x21
Write one character (in R0[7:0]) to console
Now the console will print out char “H”.
Address Hex ASCII
x3007 x0048 “H”
x3008 x0065 “e”
x3009 x006C “l”
x300A x006C “l”
x300B x006F “o”
x300C x0000 “NULL”
R0 x0048
R1 x3007
R2 x0000
R3 x0000
R4 x0000
R5 x0000
Solution 1
ADD R1, R1, #1
Remember R1 stored the address of first char of the string “Hello”.
This instruction will increase the address by one.
Address Hex ASCII
x3007 x0048 “H”
x3008 x0065 “e”
x3009 x006C “l”
x300A x006C “l”
x300B x006F “o”
x300C x0000 “NULL”
R0 x0048
R1 x3008
R2 x0000
R3 x0000
R4 x0000
R5 x0000
R0 x0048
R1 x3007
R2 x0000
R3 x0000
R4 x0000
R5 x0000
Solution 1
BRnzp loop
Branch to label “loop” if either one of condition code register N, Z and P is set.
ALWAYS branch to “loop”.
Solution 1
The program will keep going back to label “loop” and print out string “Hello”.
Until the “NULL” char (0x0000) is loaded to R0,
Then the next instruction “BRz finish” will take the effect.
After that, the instruction “finish HALT” will halt the program.
Question!
What will be output to the console when we run this assembly program?
.ORIG x3000
AND R0, R0, #0
AND R1, R1, #0
ADD R1, R1, #7
NOT R1, R1
ADD R1, R1, #1
LD R2, CONST
ADD R0, R1, R2
LD R1, ASCII
ADD R0, R0, R1
OUT
HALT
CONST .FILL x10
ASCII .FILL x30
.END
Question!
What will be output to the console when we run this assembly program?
.ORIG x3000
AND R0, R0, #0
AND R1, R1, #0
ADD R1, R1, #7
NOT R1, R1
ADD R1, R1, #1
LD R2, CONST
ADD R0, R1, R2
LD R1, ASCII
ADD R0, R0, R1
OUT
HALT
CONST .FILL x10
ASCII .FILL x30
.END
Clear the registers
Question!
What will be output to the console when we run this assembly program?
.ORIG x3000
AND R0, R0, #0
AND R1, R1, #0
ADD R1, R1, #7
NOT R1, R1
ADD R1, R1, #1
LD R2, CONST
ADD R0, R1, R2
LD R1, ASCII
ADD R0, R0, R1
OUT
HALT
CONST .FILL x10
ASCII .FILL x30
.END
Place 7 into R7
Question!
What will be output to the console when we run this assembly program?
.ORIG x3000
AND R0, R0, #0
AND R1, R1, #0
ADD R1, R1, #7
NOT R1, R1
ADD R1, R1, #1
LD R2, CONST
ADD R0, R1, R2
LD R1, ASCII
ADD R0, R0, R1
OUT
HALT
CONST .FILL x10
ASCII .FILL x30
.END
Take the two’s complement of R1, to negate the number for subtraction
Question!
What will be output to the console when we run this assembly program?
.ORIG x3000
AND R0, R0, #0
AND R1, R1, #0
ADD R1, R1, #7
NOT R1, R1
ADD R1, R1, #1
LD R2, CONST
ADD R0, R1, R2
LD R1, ASCII
ADD R0, R0, R1
OUT
HALT
CONST .FILL x10
ASCII .FILL x30
.END
Place a pre-defined constant (16) into R2
Question!
What will be output to the console when we run this assembly program?
.ORIG x3000
AND R0, R0, #0
AND R1, R1, #0
ADD R1, R1, #7
NOT R1, R1
ADD R1, R1, #1
LD R2, CONST
ADD R0, R1, R2
LD R1, ASCII
ADD R0, R0, R1
OUT
HALT
CONST .FILL x10
ASCII .FILL x30
.END
Add R1 and R2, and store the result in R0
Question!
What will be output to the console when we run this assembly program?
.ORIG x3000
AND R0, R0, #0
AND R1, R1, #0
ADD R1, R1, #7
NOT R1, R1
ADD R1, R1, #1
LD R2, CONST
ADD R0, R1, R2
LD R1, ASCII
ADD R0, R0, R1
OUT
HALT
CONST .FILL x10
ASCII .FILL x30
.END
Adjust R0 so that the number is printed correctly
Question!
What will be output to the console when we run this assembly program?
.ORIG x3000
AND R0, R0, #0
AND R1, R1, #0
ADD R1, R1, #7
NOT R1, R1
ADD R1, R1, #1
LD R2, CONST
ADD R0, R1, R2
LD R1, ASCII
ADD R0, R0, R1
OUT
HALT
CONST .FILL x10
ASCII .FILL x30
.END
Print the contents of R0 to the console, and then stop
/docProps/thumbnail.jpeg