Chapter 6 Programming in Machine Language The LC3










![Example: Program to multiply [R 4] x [R 5] and place the result in Example: Program to multiply [R 4] x [R 5] and place the result in](https://slidetodoc.com/presentation_image_h2/9364af8b94d1679c847fef8306a81ce8/image-11.jpg)

![Example: Program to multiply [R 4] x [R 5] and place the result in Example: Program to multiply [R 4] x [R 5] and place the result in](https://slidetodoc.com/presentation_image_h2/9364af8b94d1679c847fef8306a81ce8/image-13.jpg)
![Example: Program to multiply [R 4] x [R 5] and place the result in Example: Program to multiply [R 4] x [R 5] and place the result in](https://slidetodoc.com/presentation_image_h2/9364af8b94d1679c847fef8306a81ce8/image-14.jpg)
![Example: Program to multiply [R 4] x [R 5] and place the result in Example: Program to multiply [R 4] x [R 5] and place the result in](https://slidetodoc.com/presentation_image_h2/9364af8b94d1679c847fef8306a81ce8/image-15.jpg)






![Example: Program to multiply [R 4] x [R 5] and place the result in Example: Program to multiply [R 4] x [R 5] and place the result in](https://slidetodoc.com/presentation_image_h2/9364af8b94d1679c847fef8306a81ce8/image-22.jpg)
![Example: Program to multiply [R 4] x [R 5] and place the result in Example: Program to multiply [R 4] x [R 5] and place the result in](https://slidetodoc.com/presentation_image_h2/9364af8b94d1679c847fef8306a81ce8/image-23.jpg)








- Slides: 31

Chapter 6 • Programming in Machine Language • The LC-3 Simulator • The LC-3 Editor

The LC-3 Computer a von Neumann machine The Instruction Cycle: Fetch: Next Instruction from Memory (PC) (points to) next instruction PC (PC) + 1 Decode: Fetched Instruction Evaluate: Instr & Address (es) (find where the data is) Load: PSW Operand (s) (get data as specified) Execute: Operation Store: Result (if specified) PSW (Program Status Word): Bits: 15 | S| 10 9 8 |Priority| Memory 2 1 0 | N| Z| P |

Important Registers in the LC-3 CPU Registers: • 8 General Purpose Registers (R 0 – R 7) – Holds Data or Addresses • Program Counter (PC) - Points to the next instruction • Instruction Register (IR) – holds the instruction being executed • Program Status Word (PSW) – holds the status of the program being executed, including N Z P: Negative, Zero, Positive result of an operate instruction Memory Access Registers: • Memory Address Register (MAR) – Holds the address of a memory location being accessed • Memory Data Register (MDR) – Hold the data to be written into memory or the date read from memory Note: These are all 16 bit registers

LC-3 Memory Map (64 K of 16 bit words) 256 words (We will get to these later) 23. 5 K words 39. 5 K words 512 words

LC-3 Instructions Addressing Modes • Register (Operand is in one of the 8 registers) • PC-relative (Operand is “offset” from where the PC points - offsets are sign extended to 16 bits) • Base + Offset (Base relative) (Operand is “offset” from the contents of a register) • Immediate (Operand is in the instruction) • Indirect (The “Operand” points to the real address of Operand – rather than being the operand) Note: The LC-3 has No Direct Addressing Mode

Operate Instructions • There are only three operate Instructions: - ADD Register mode Register/Immediate mode - AND - NOT • Register mode Register/Immediate mode Register mode [0001 DR SR 1 0 00 SR 2] [0001 DR SR 1 1 imm 5] [0101 DR SR 1 0 00 SR 2] [0101 DR SR 1 1 imm 5] [1001 DR SR 111111] The Source and Destination operands are: CPU Registers or Immediate Values

Data Movement Instructions • Load - read data from memory to a register – LD: PC-relative mode [0010 DR PCoffset 9] – LDI: Indirect mode [1010 DR PCoffset 9] – LDR: Base+offset mode [0110 DR Base. R offset 6] • Store - write data from a register to memory – ST: PC-relative mode [0011 DR PCoffset 9] – STI: Indirect mode [1011 DR PCoffset 9] – STR: Base+offset mode [0111 DR Base. R offset 6] • Load effective address – address saved in register – LEA: PC-relative mode [1110 DR PCoffset 9] All have 2 or 3 operands

Control Instructions • Go to New Location in Program – “GO TO” – BR: – JMP: PC-relative mode Indirect mode • Trap Service Routine Call – TRAP: Indirect • Jump to Subroutine (will be covered later) – JSR: – JSRR: PC-relative mode Indirect mode • Return from Trap/Subroutine – RET: No operand [0000 NZP PCoffset 9] [1100 000 Base. R 000000] [1111 0000 Trap. Vec 8] [0100 1 PCoffset 11] [0100 000 Base. R 000000] [1100 000 111 000000] • Return from Interrupt (will be covered later) – RTI: No operand [1000 000000]

TRAP Instruction • Calls a service routine, identified by 8 -bit “trap vector. ” vector • • • Service routine (Partial List) x 23 input a character from the keyboard x 21 output a character to the monitor x 25 halt the program Register R 7 is loaded with the incremented contents of the PC. The PC is loaded with the address in the Trapvector Table at position “trapvector 8” R 0 is typically used for passing values between the Program and the Trap Routine RET [1100 000 111 000000] • When service routine is done, an RET will load R 7 (the incremented value of the PC before jumping to the TRAP routine) into the PC, and the program will continue with the next instruction after the TRAP, i. e. the program will “return” from the TRAP Routine. Note: an RET is a JMP Base-relative with Base = R 7

TRAPS See page 543.
![Example Program to multiply R 4 x R 5 and place the result in Example: Program to multiply [R 4] x [R 5] and place the result in](https://slidetodoc.com/presentation_image_h2/9364af8b94d1679c847fef8306a81ce8/image-11.jpg)
Example: Program to multiply [R 4] x [R 5] and place the result in R 2 clear R 2 add R 4 to R 2 decrement R 5 No R 5 = 0? R 4 – Multiplicand Yes HALT R 5 – Multiplier R 2 – Accumulator

LC-3 Instructions Addressing Modes • Register (Operand is in one of the 8 registers) • PC-relative (Operand is “offset” from where the PC points - offsets are sign extended to 16 bits) • Base + Offset (Base relative) (Operand is “offset” from the contents of a register) • Immediate (Operand is in the instruction) • Indirect (The “Operand” points to the real address of Operand – rather than being the operand) Note: The LC-3 has No Direct Addressing Mode
![Example Program to multiply R 4 x R 5 and place the result in Example: Program to multiply [R 4] x [R 5] and place the result in](https://slidetodoc.com/presentation_image_h2/9364af8b94d1679c847fef8306a81ce8/image-13.jpg)
Example: Program to multiply [R 4] x [R 5] and place the result in R 2 clear R 2 add R 4 to R 2 decrement R 5 No R 5 = 0? Yes HALT R 2 <- 0 R 2 <- R 2 + R 4 R 5 <- R 5 – 1 BRz x 3201 HALT x 3200 ?
![Example Program to multiply R 4 x R 5 and place the result in Example: Program to multiply [R 4] x [R 5] and place the result in](https://slidetodoc.com/presentation_image_h2/9364af8b94d1679c847fef8306a81ce8/image-14.jpg)
Example: Program to multiply [R 4] x [R 5] and place the result in R 2 clear R 2 add R 4 to R 2 decrement R 5 No R 5 = 0? Yes HALT R 2 <- 0 R 2 <- R 2 + R 4 R 5 <- R 5 – 1 BRz x 3201 HALT x 3200 x 3201 x 3202 x 3203 x 3204 01010100000 00010100100 0001101101111111 0000010111111101 1111000000100101
![Example Program to multiply R 4 x R 5 and place the result in Example: Program to multiply [R 4] x [R 5] and place the result in](https://slidetodoc.com/presentation_image_h2/9364af8b94d1679c847fef8306a81ce8/image-15.jpg)
Example: Program to multiply [R 4] x [R 5] and place the result in R 2 clear R 2 add R 4 to R 2 decrement R 5 No R 5 = 0? Yes HALT R 2 <- 0 R 2 <- R 2 + R 4 R 5 <- R 5 – 1 BRzp x 3201 HALT x 3200 x 3201 x 3202 x 3203 x 3204 01010100000 00010100100 0001101101111111 0000010111111101 1111000000100101 54 A 0 1484 1 B 7 F 02 FD FO 25

LC 3 Editor / Simulator Go to Author’s Web Site: http: //www. mhhe. com/patt 2 Get: • LC 3 Edit • LC 3 Simulator

LC 3 Edit Screen

LC 3 Edit Enter (or Load) the program into LC 3 Edit - Store it as prog. bin for a binary file, or Store it as prog. hex for a hex file - Create a prog. obj file with the Editor

LC-3 Simulator Screen

LC-3 Simulator Open LC-3 Simulator - Load prog. obj - Load data. obj - Set breakpoint(s) or - Step through program

LC-3 Simulator Open LC-3 Simulator - Load prog. obj - Load data. obj - Initialize values (PC, memory, registers) - Set breakpoint(s) - Step through program checking registers and “memory map” - Debug program
![Example Program to multiply R 4 x R 5 and place the result in Example: Program to multiply [R 4] x [R 5] and place the result in](https://slidetodoc.com/presentation_image_h2/9364af8b94d1679c847fef8306a81ce8/image-22.jpg)
Example: Program to multiply [R 4] x [R 5] and place the result in R 2 clear R 2 add R 4 to R 2 decrement R 5 No R 5 = 0? Yes HALT R 2 <- 0 R 2 <- R 2 + R 4 R 5 <- R 5 – 1 BRzp x 3201 HALT x 3200 x 3201 x 3202 x 3203 x 3204 01010100000 00010100100 0001101101111111 000001111101 1111000000100101 54 A 0 1484 1 B 7 F 03 FD FO 25
![Example Program to multiply R 4 x R 5 and place the result in Example: Program to multiply [R 4] x [R 5] and place the result in](https://slidetodoc.com/presentation_image_h2/9364af8b94d1679c847fef8306a81ce8/image-23.jpg)
Example: Program to multiply [R 4] x [R 5] and place the result in R 2 Enter Program in Simulator and test it: • Enter Program • Run • Single Step • Add Breakpoints

The Sum program in “binary” x 3000 x 3001 x 3002 x 3003 x 3004 x 3005 x 3006 x 3007 x 3008 x 3009 x 300 A 0011000000 111000101111 0101011011100000 01010100000 00010101100 0000010000000101 01101000000 000101101100010010011000010111111 000011111010 1111000000100101 ; start x 3000 ; R 1=x 3100 ; R 3=0 ; R 2=R 2+12 ; If z goto x 300 A ; Load next value into R 4 ; R 3=R 3+R 4 ; R 1=R 1+1 ; R 2=R 2 -1 ; goto x 3004 ; halt

The Sum program in “hex” x 3000 x 3001 x 3002 x 3003 x 3004 x 3005 x 3006 x 3007 x 3008 x 3009 x 300 A 3000 E 2 FF 56 E 0 54 A 0 14 AC 0405 6840 16 C 4 1261 14 BF 0 FFA F 025 ; start x 3000 ; R 1=x 3100 ; R 3=0 ; R 2=R 2+12 ; If z goto x 300 A ; Load next value into R 4 ; R 3=R 3+R 4 ; R 1=R 1+1 ; R 2=R 2 -1 ; goto x 3004 ; halt

The Sum program Data in “hex” x 3100 x 3101 x 3102 x 3103 x 3104 x 3105 x 3106 x 3107 x 3108 x 3109 x 310 A x 310 B 3100 0001 0002 0004 0008 FFFF 1 C 10 11 B 1 0019 0 F 07 0004 0 A 00 400 F ; Begin data at x 3100 ; Loc x 310 B

Example Compute the Sum of 12 Integers Program • • Program begins at location x 3000. Integers begin at location x 3100. R 1 x 3100 R 3 0 (Sum) R 2 12(count) R 2=0? NO R 4 R 3 R 1 R 2 M[R 1] R 3+R 4 R 1+1 R 2 -1 YES R 1: “Array” index pointer (Begin with location 3100) R 3: Accumulator for the sum of integers R 2: Loop counter (Count down from 12) R 4: Temporary register to store next integer

Example: Compute the Sum of 12 Integers Program Enter Program in Simulator and test it: • Enter Program • Enter Data • Run • Single Step • Add Breakpoints

Example: # of Occurrences of an Inputted Char from a string R 3 – ptr to char string, R 1 – input char buffer, R 2 – char count, R 0 – output buffer

Example: Counting Occurrences of a Character Address Instruction Comments x 3000 0 1 0 1 0 1 0 0 0 R 2 0 (counter) x 3001 0 0 1 1 0 0 0 0 R 3 M[x 3102] (ptr) x 3002 1 1 0 0 0 1 1 Input to R 0(TRAP x 23) x 3003 0 1 1 0 0 0 R 1 M[R 3] x 3004 0 0 0 1 1 1 0 0 R 4 R 1 – 4 (EOT) x 3005 0 0 0 1 1 1 0 If Z, goto x 300 E x 3006 1 0 0 1 1 1 1 R 1 NOT R 1 x 3007 0 0 0 1 1 0 0 1 R 1 + 1 X 3008 0 0 0 1 0 0 0 R 1 + R 0 x 3009 0 0 1 0 1 1 If N or P, goto x 300 B x 300 A 0 0 0 1 0 1 0 0 1 R 2 + 1 x 300 B 0 0 0 1 1 1 0 0 1 R 3 + 1 x 300 C 0 1 1 0 0 0 R 1 M[R 3] x 300 D 0 0 1 1 1 0 0 0 1 0 0 Goto x 3004 x 300 E 0 0 1 0 0 1 1 R 0 M[x 3013] x 300 F 0 0 0 1 0 0 0 0 0 1 0 R 0 + R 2 x 3010 1 1 0 0 0 1 Print R 0 (TRAP x 21) x 3011 1 1 0 0 0 1 HALT (TRAP x 25) X 3012 Starting Address of File x 3013 0 0 0 0 0 1 1 0 0 ASCII x 30 (‘ 0’) R 3 – ptr to char string, R 1 – input char buffer, R 2 – char count, R 0 – Keyboard char /Output display char

HW 6: After reading the Simulator manual: 1) Write a program to place the absolute value of the [R 2] in R 2. The program should begin in memory location 3000 Test it on the LC-3 Simulator. Provide Simulator Snapshots with your homework. 2) Write a program to read a value of N from memory and calculate N factorial. The result should be in R 4. The program should begin in memory location 3050 N should be in memory location 3000 Test the program on the LC-3 Simulator Provide Simulator Snapshots with your homework.