Overview Program in LC3 machine language Use the

  • Slides: 21
Download presentation
Overview Program in LC-3 machine language Use the Editor Use LC-3 Simulator Trap Instruction

Overview Program in LC-3 machine language Use the Editor Use LC-3 Simulator Trap Instruction

LC-3 Editor / Simulator Go to Authors’ Web page: http: //www. mhhe. com/patt 2

LC-3 Editor / Simulator Go to Authors’ Web page: http: //www. mhhe. com/patt 2 Download: • LC-3 Simulator – Win or Unix - (LC-3 Edit, LC-3 Simulate) • LC-3 Simulator Lab Manual

Sum of 12 Integers • Compute sum of 12 integers. Numbers start at location

Sum of 12 Integers • Compute sum of 12 integers. Numbers start at location x 3100. Program starts at location x 3000. R 1 x 3100 R 3 0 (Sum) R 2 12(count) R 2=0? NO R 4 R 3 R 1 R 2 YES Write program M[R 1] R 3+R 4 R 1+1 R 2 -1

Sum integers from x 3100 – x 310 B Address Instruction Comments x 3000

Sum integers from x 3100 – x 310 B Address Instruction Comments x 3000 1 1 1 0 0 0 1 1 1 1 1 R 1 x 3100 x 3001 0 1 0 1 1 1 0 0 0 R 3 0 x 3002 0 1 0 1 0 1 0 0 0 R 2 0 x 3003 0 0 0 1 0 1 0 1 1 0 0 R 2 12 x 3004 0 0 0 1 0 0 0 0 1 If Z, goto x 300 A x 3005 0 1 1 0 0 0 0 1 0 0 0 Load next value to R 4 x 3006 0 0 0 1 1 0 0 0 1 0 0 x 3007 0 0 0 1 1 0 0 1 Increment R 1 (pointer) X 3008 0 0 0 1 0 1 1 1 1 Decrement R 2 (counter) x 3009 0 0 1 1 1 1 1 0 Goto x 3004 Add to R 3

The Add program in “binary” 0011000000 111000101111 0101011011100000 01010100000 00010101100 0000010000000101 01101000000 000101101100010010011000010111111 000011111010

The Add program in “binary” 0011000000 111000101111 0101011011100000 01010100000 00010101100 0000010000000101 01101000000 000101101100010010011000010111111 000011111010 ; start x 3000 ; R 1=x 3100 ; R 3=0 ; R 2=R 2+12 ; If z goto x 3009 ; 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

The Add program in “hex” 3000 ; start x 3000 E 2 FF ;

The Add program in “hex” 3000 ; start x 3000 E 2 FF ; R 1=x 3100 56 E 0 ; R 3=0 54 A 0 ; R 2=0 14 AC ; R 2=R 2+12 0405 ; If z goto x 3009 D 840 ; Load next value into R 4 16 C 4 ; R 3=R 3+R 4 1261 ; R 1=R 1+1 14 BF ; R 2=R 2 -1 0 FFA ; goto x 3004

The Add program Data (hex) 3100 ; Begin data at x 3100 3107 ;

The Add program Data (hex) 3100 ; Begin data at x 3100 3107 ; Loc x 3100 2819 0110 0310 0110 11 B 1 0019 0007 0004 0000 ; Loc x 310 B

LC 3 Edit • Load the program into LC 3 Edit - Store it

LC 3 Edit • 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 • Load the data into LCEdit - Store it as data. bin for a binary file, or Store it as data. hex for a hex file - Create a data. obj file with the Editor

LC-3 Simulator • Open LC-3 Simulator - Load prog. obj - Load data. obj

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

TRAP • Calls a service routine, identified by 8 -bit “trap vector. ” vector

TRAP • Calls a service routine, identified by 8 -bit “trap vector. ” vector routine x 23 input a character from the keyboard x 21 output a character to the monitor x 25 halt the program • When routine is done, PC is set to the instruction following TRAP.

TRAPS

TRAPS

Add HALT to “Add” program • Add HALT statement and run program without breakpoint(s)

Add HALT to “Add” program • Add HALT statement and run program without breakpoint(s)

Example : Multiply Write program to Multiply two unsigned integers in R 4 and

Example : Multiply Write program to Multiply two unsigned integers in R 4 and R 5

Example : Multiply • This program multiplies two unsigned integers in R 4 and

Example : Multiply • This program multiplies two unsigned integers in R 4 and R 5. clear R 2 add R 4 to R 2 decrement R 5 No R 5 = 0? Yes HALT Write program

Program to multiply [R 4] x [R 5] and place result in R 2

Program to multiply [R 4] x [R 5] and place result in R 2 x 3200 Clear Accumulator (R 2) R 2 <- 0 x 3201 Add [R 4] to Accumulator R 2 <- R 2 + R 4 x 3202 Dec R 5 <- R 5 – 1 x 3203 Do again if [R 5] > 0 BR p x 3201 x 3204 Stop HALT

Program to multiply [R 4] x [R 5] and place result in R 2

Program to multiply [R 4] x [R 5] and place result in R 2 Code program

Program to multiply [R 4] x [R 5] and place result in R 2

Program to multiply [R 4] x [R 5] and place result in R 2 x 3200 Clear Accumulator (R 2) R 2 <- 0 0101 010 1 00000 54 A 0 x 3201 Add [R 4] to Accumulator R 2 <- R 2 + R 4 0001 010 0 00 1484 x 3202 Dec R 5 <- R 5 – 1 0001 101 1 11111 1 B 7 F x 3203 Do again if [R 5] > 0 BR p x 3201 0000 001 111111101 03 FD x 3204 Stop HALT 1111 0000 00100101 F 025 Test on Simulator

Write a program to place the absolute value of the [R 2] in R

Write a program to place the absolute value of the [R 2] in R 2 How do we compute A=-A if we don’t have a subtract instruction?

Example - # Occurrences of Inputted Char

Example - # Occurrences of Inputted Char

Example - # Occurrences of Inputted Char Program (1 of 2) Address Instruction Comments

Example - # Occurrences of Inputted Char Program (1 of 2) Address Instruction Comments x 3000 0 1 0 1 0 1 0 0 0 R 2 0 (counter) x 3001 0 0 1 0 0 1 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

Example - # Occurrences of Inputted Char Program (2 of 2) Address Instruction Comments

Example - # Occurrences of Inputted Char Program (2 of 2) Address Instruction Comments 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’)