150 likes | 287 Views
COMPSCI 210 Semester 1 - 2014. Tutorial 6 – Revision. Solution – Exercise 5.13. ADD R3, R2, #0 NOT R3, R3 ADD R3, R3, #1 ADD R1, R2, R3 (c) ADD R1, R1, #0. (e) AND R2, R2, #0. Exercise 6.a. Write LC-3 programmes to perform the following Operations :
E N D
COMPSCI 210Semester 1 - 2014 Tutorial 6 – Revision
Solution – Exercise 5.13 ADD R3, R2, #0 NOT R3, R3 ADD R3, R3, #1 ADD R1, R2, R3 (c) ADD R1, R1, #0 (e) AND R2, R2, #0
Exercise 6.a Write LC-3 programmes to perform the following Operations : Subtraction 5 - 3 Multiplication 5 X 4
Solution – Exercise 6.a( 5 - 3 ) .ORIG x3000 ; subtract #5 - #3 LD R1, FIRST_NUMBER LD R2, SECOND_NUMBER ; create the negative of R2 NOT R2, R2 ADD R2, R2, #1 ; subtract - R3 now holds #2 ADD R3, R1, R2 HALT FIRST_NUMBER .fill #5 SECOND_NUMBER .fill #3 .END
Solution – Exercise 6.a(5 X 4) LD R1, FIRST_NUMBER LD R2, COUNTER ; enter loop MULTIPLY ADD R1, R1, R1 ; add to sum ADD R2, R2, #-1 ; decrement our counter BRpMULTIPLY ; continue until the counter num is 0 HALT FIRST_NUMBER.FILL #5 COUNTER .FILL #2 .END
Exercise 6.b Write an LC-3 programme to perform the following : if (a == b) { //do something1 } else { //do something2 }
Solution – Exercise 6.b ;variable 'a' is in R1 ;variable 'b' is in R2 ;subtract a from b, if it is 0 they are equal ;negate R2 and store it in R0 NOT R0, R2 ADD R0, R0, #1 ADD R0, R1, R0 ;subtract a from b BRnpELSE ;if they are not 0, go to the else block;do something1 BRnzpEND ELSE ;do something2 END:
Solution – Exercise 5.23 ( LEA R1, #1 ) ( LDR R2, R1, #2 ) ( TRAP 0x25 )
Solution – Exercise 5.25 • NOT R4, R3 • ADD R4, R4, #1 • ADD R1, R4, R2 • BRz Done • BRn Reg3 • BRp Reg2 • Reg3 ADD R1, R3, #0 • BRnzp Done • Reg2 ADD R1, R2, #0 • Done TRAP 0x25
Solution – Exercise 7.9 TRAP 0x25 has the same effect as using HALT