LC-3 Code Samples

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Character->Value Uses: LD, IN, OUT, ADD, BRnp, .FILL

Convert a character to its true value In this sample we want to input a single character, and convert it to its equivalent value: character "0" == 0, character "1" == 1, etc. If the input character is "0" quit. Otherwise keeping looping and getting input. Our strategy: since ascii codes for 0, 1, 2, 3... are sequential, we can subtract the ascii code for "0" from the code for the input character to achieve a 'value' equivalent for the character: ( ascii "1" code - ascii "0" code) == 1. Copy paste the code below into a simulator like http://wchargin.github.io/lc3web/ ------------------- cut here --------------------------- .ORIG x3000 LOOP_START LD R0, NEWLINE; load the VALUE at NEWLINE OUT; print the value in R0 (newline) IN; get ONE character into R0 LD R1, ASCII_BASE; load the VALUE at ASCII_BASE ADD R0, R0, R1; add the negative base (subtract) ; now R0 (above) has a VALUE BRnp LOOP_START; jump back to the start if NOT zero (if negative or positive) HALT NEWLINE .FILL x000D ; store the newline character ASCII_BASE .FILL #-48; store the negative ascii code for "0" .END

Sample 1: Hello World Uses: LEA, PUTS, BR, STRINGZ

Hello World A Simple "Hello World" program in LC-3 that prints infinitely. Copy paste the code below into a simulator like http://wchargin.github.io/lc3web/ ------------------- cut here --------------------------- .ORIG x3000 ; code start START ; just a label LEA R0, HI ; load the address of the "HI" PUTS ; print hello world BR START ; jump back to start HALT HI .STRINGZ "Hello World!\n" ; your message .END ; code end

Looping Times 10 Uses: LD, ADD, BRnp, HALT, .FILL

Looping Times 10 Here we will load an initial value into R0, then loop 10 times, adding R0 to an 'accumulator' register R3 each time. The result in R3 will be "10 times R0". Note that the HALT trap will overwrite R0 & R1, so we use R3 to ensure you can inspect the final result after completion. Copy paste the code below into a simulator like http://wchargin.github.io/lc3web/ ------------------- cut here --------------------------- .ORIG x3000 LD R0, VALUE ; load the value we want to multiple LD R3, ZERO; stort with zero in R3 LD R2, TEN; load the loop max LOOP_START ADD R3, R3, R0; add R0 for each loop ADD R2, R2, #-1; decrement R2 each loop BRnp LOOP_START; loop back if last operation was not zero (R2) ; else we are done HALT; R3 should have our result (R0, R1 will be changed by the trap) VALUE .FILL #3; the value 0 TEN .FILL #10; the value 10 ZERO .FILL #0; the value zero .END

Sample 2: Mimic Uses: LABEL, IN, BR

Mimic A Simple program in LC-3 that asks for a character, and prints it back...forever. Note that a "newline" character is never echoed back, unless the user types it as input.... how would you fix that? (see Mimic 2) Copy paste the code below into a simulator like http://wchargin.github.io/lc3web/ ------------------- cut here --------------------------- .ORIG x3000 ; code start START ; just a label IN BR START ; jump back to start .END ; code end

Sample 3: Mimic 2 Uses: IN, OUT, LD, BR

Mimic 2 A better version of the Mimic sample that asks for a character, and prints it back...followed by a newline character. Copy paste the code below into a simulator like http://wchargin.github.io/lc3web/ ------------------- cut here --------------------------- .ORIG x3000 ; code start START ; just a label IN ; prompt, wait, and echo 1 character LD R0, NEWLINE ; load the NEWLINE VALUE into R0 OUT ; print the value from R0 BR START ; jump back to start NEWLINE .FILL x000D ; store the newline character .END ; code end

Negate a Value Uses: LD, NOT, ADD

Negate a Value Invert the bits, and add one! Copy paste the code below into a simulator like http://wchargin.github.io/lc3web/ ------------------- cut here --------------------------- .ORIG x3000 LD R0, MY_VALUE; load the value at MY_VALUE into R0 NOT R1, R0; invert the bits of R0 and store in R1 ADD R2, R1, #1 ; add one HALT; we are done - R2 should hold positive 123 MY_VALUE .FILL #-123 .END

Sample 4: Simple Loop Uses: LD, LEA, PUTS, ADD, BRp

Sample 4: Implements a simple loop that counts down from 10 (or any number you choose to store). .ORIG x3000 LD R1, LOOPMAX; load value at LOOPMAX into R1 LD R2, NEG_ONE; load value at NEG_ONE into R2 LEA R0, LOOP_MSG; load ADDRESS of LOOP_MSG into R0 LOOP_START PUTS; print the looping message ADD R1, R2, R1; decrement the R1 loop counter ;NOTE: ADD R1, #-1 would also work above! BRp LOOP_START; if R1 is still positive, loop again LOOP_END; else we are done.. this tag really does nothing LEA R0, DONE_MSG; load the address of our done message PUTS; print the done message HALT LOOPMAX .FILL x000A ; hex for 10 NEG_ONE .FILL #-1 ; decimal for -1 LOOP_MSG .STRINGZ "Looping...\n" DONE_MSG .STRINGZ "All Done!\n" .END

Sample Template

Sample Template: Every LC-3 code sample starts this way (below). Remember that semi-colon ";" starts a comment on any line! Copy paste the code below into a simulator like http://wchargin.github.io/lc3web/ ------------------- cut here --------------------------- .ORIG x3000 ; code start ; >your code in here< HALT; stop execution of any code here ; store data here: strings, values, etc. .END ; code end

Shift left Uses: LD, ADD, .FILL

Shift Left (multiply by 2) Shift all bits of register R0 "one bit to its left This is the same as multiplying R0 by 2! (which is the same as adding a value to itself) Note that if the leftmost bit was already a 1, we will have 'overflow'. Copy paste the code below into a simulator like http://wchargin.github.io/lc3web/ ------------------- cut here --------------------------- .ORIG x3000 LD R0, ONE; load R0 with the value at label ONE ADD R0, R0, R0; double the value HALT ONE .FILL #1; the value of ONE .END

Simple Function Call Uses: JSR, RET, BR, ADD, IN, OUT

Simple Function Call Here we will use the JSR opcode to jump to a function via a label, storing the 'return address' in R7. NOTE: Any traps you use (IN, OUT, PUTS, etc) in your 'function' will overwrite R7, so you need to save R7 somewhere right away in your function, if you plan to use traps. The sample below loops infinitely, calling the "MY_FUNC" function label, which gets and writes a single character before returning to 'main'. Copy paste the code below into a simulator like http://wchargin.github.io/lc3web/ ------------------- cut here --------------------------- .ORIG x3000 LOOP_START JSR MY_FUNC; jump to the MY_FUNC label BR LOOP_START; repeat MY_FUNC ADD R5, R7, #0; copy the 'return' location of R7 into R5, since it will get stomped by IN below IN; get a character LD R0, NEWLINE; get the newline character into R0 OUT; print newline ADD R7, R5, #0; restore the original R7 value RET; return from the call, or we could have done a JMP R5! NEWLINE .FILL x000D; this is the newline character .END

Simple Times 10 Uses: LD, ADD, .FILL

Simple Times 10 Here we will use a brute-force approach to multiply a stored value (in this case #3) by 10. We'll place the result in the R2 register when done. NOTE: our HALT is a trap and will change the values of R0 and R1 at program termination. Be sure to "NEXT" or step through the program to see the results in those registers before they are overwritten. R2 should remain in tact. Copy paste the code below into a simulator like http://wchargin.github.io/lc3web/ ------------------- cut here --------------------------- .ORIG x3000 LD R0, VALUE; load the value at VALUE into R0 ADD R0, R0, R0; double it (R0=2x) ADD R1, R0, R0; double again (R1 = 4x) ADD R1, R1, R1; double again (R1 = 8x) ADD R2, R1, R0; add 2x (R2 = 10x) ; R2 should have #30 which is hex x001E VALUE .FILL #3; decimal 3 .END

BIT MASK Uses: LD, AND, BRz, LEA, PUTS, HALT, .FILL, .STRINGZ

BIT MASK The sample below loads a value from memory and tests if a particular bit of its binary representation is ONE or ZERO. Our Strategy: use a 'mask' which we know has a specific bit set to one. "AND'ing" this mask with our value will produce a new value that is only zero, if the value did NOT have a "1" in the same bit as our mask. Thus telling us if our value's corresponding bit was set or not! NOTE! We are testing a negative number below: decimal -5. A positive 5 would be represented as b101 (1 'four' plus 1 'one'). But since we are dealing in 2's compliment for negative numbers, binary for #-5 will be "1111111111111011". So bit 3 will be ZERO. If you change the value to +5, the result will be ONE Copy paste the code below into a simulator like http://wchargin.github.io/lc3web/ ------------------- cut here --------------------------- .ORIG x3000 LD R0, VALUE; get the value we want to test LD R1, MASKb3; get the mask for bit 3 AND R2, R1, R0; 'AND' the registers BRz IS_ZERO; if the AND was zero, jump to IS_ZERO label ; else it must be one... LEA R0, BIT_IS_ONE; load the not set message PUTS; print it HALT; stop the program IS_ZERO LEA R0, BIT_IS_ZERO; load the is set message PUTS; print it HALT; stop the program VALUE .FILL "#-5"; negative 5 in binary is b1111111111111011 (2's compliment) MASKb1 .FILL b0000000000000001; a mask for bit 1 (least significant bit) MASKb3 .FILL b0000000000000100; a mask for bit 3 MASKb16 .FILL b1000000000000000; a mast for bit 16 (most significant bit) BIT_IS_ONE .STRINGZ "\nThat bit is ONE"; BIT_IS_ZERO .STRINGZ "\nThat bit is ZERO\n"; .END


Ensembles d'études connexes

General Eduacation for Vertiv Thermal and Power Units

View Set

Data Structures Midterm CHAPTER 8

View Set

C243 Ch. 8 Intercompany Indebtedness

View Set

Impact of European Colonialism in the Americas

View Set