r/lc3 • u/toasterstove • May 30 '19
r/lc3 • u/Infamous_Hedgehog595 • May 02 '23
LC3 assembly program which calculates the sum of all odd numbers or all even numbers stored between x3200 and x32FF inclusive. Program will use LC3 input / output to determine if odd values or even values are being summed
Can someone help me fixing my code
Program Details
Your program will start at x3000 in memory and sum all odd or even numbers stored at memory locations x3200 to x32FF. Please note that while the autograder will populate these memory address with data values, when testing your program, you will need to place these values into memory manually. I recommend doing so by modifying the memory values via the simulator rather than using assembly code. Because the range of values is so large, I recommend placing a few values at the start of the memory range and a few at the end leaving the middle addresses empty. This will allow you to both confirm that your program is covering all necessary memory locations and also produce a result which is easy to verify. When run, your program should start by printing out the following message: "Please enter a 1 to sum all odd numbers. Otherwise all even numbers will be summed\n". Your program will then use the KBSR and KBDR to capture the users’ input. As stated in the prompt, if the input value is a 1, the program should output the sum of all odd numbers. If the input value is not a 1, it should output the sum of all even numbers. The final sum will be stored at memory location x3300. Once stored, your program will halt the machine using the HALT command. You must place any .FILL pseudo-ops after your HALT command. Placing .FILL pseudo- ops before the HALT will cause the data stored at those locations to be executed as instructions. This can cause your code to produce unexpected results and prevent from executing correctly My code
.ORIG x3000
; prompt user for input
LEA R0, prompt
PUTS
GETC ; read user input
OUT ; echo user input
LD R1, input_mask
AND R0, R0, R1 ; mask out input
BRz even_sum ; branch if input is even
; odd sum
LD R2, mem_start ; load first memory address to R2
odd_loop ADD R3, R2, #0 ; copy current memory address to R3
AND R3, R3, R1 ; check if current value is odd
BRz odd_done ; branch if even
; add odd value to sum
LDR R4, R2, #0 ; load current value
ADD R5, R5, R4 ; add to sum
odd_done ADD R2, R2, #1 ; increment memory address
ADD R2, R2, #1
ADD R2, R2, #1
ADD R2, R2, #1
ADD R2, R2, #1 ; increment by 5 to skip even values
ADD R2, R2, #1
ADD R2, R2, #1
ADD R2, R2, #1
ADD R2, R2, #1
BRzp odd_loop ; branch if not finished
; store odd sum
ST R5, result
BR done
; even sum
even_sum LD R2, mem_start ; load first memory address to R2
even_loop ADD R3, R2, #0 ; copy current memory address to R3
AND R3, R3, R1 ; check if current value is even
BRnp even_done ; branch if odd
; add even value to sum
LDR R4, R2, #0 ; load current value
ADD R5, R5, R4 ; add to sum
even_done ADD R2, R2, #1 ; increment memory address
ADD R2, R2, #1 ; increment by 2 to skip odd values
BRzp even_loop ; branch if not finished
; store even sum
ST R5, result
done HALT
prompt .STRINGZ "Please enter a 1 to sum all odd numbers. Otherwise all even
numbers will be summed\n"
input_mask .FILL x0002
result .FILL #0
mem_start .FILL x3200
.ENDr/lc3 • u/[deleted] • Mar 13 '17
LC3 Notepad++ Dark Theme
I liked the Notepad++ theme that this guy made. I use a dark theme for my N++ and all my IDEs though, so it didn't quite fit with it. I modified it to be dark, and specifically to match the N++ Waher Style theme.
Besides coloring, I made two changes to the style:
Instructions that set the condition code are colored differently from instructions that do not (light purple and dark purple respectively). This should make it a little easier to figure out where it was last set when you get to a branch statement.
The numbers, both decimal and hex, were not being colored correctly. To get it to work correctly I had to explicitly define all decimal numbers from -1024 to +1024. It's due to the constraints N++ gives you in defining number formats, specifically it doesn't like #- preceding a number if you define # as the prefix. You would need to use both #- and # as prefixes, which would preclude you from making a number style for hex values.
r/lc3 • u/WUTDO11231235 • Nov 10 '16
XOR in LC3?
How can I XOR two values in LC3? I'm trying to figure out a way to XOR R1 and R2 and store it in R0 and using ONLY those three registers.
I came up with X XOR Y = [(xy')' & (x'y)']'
using demoregans, but I can't figure out a way to XOR R1 and R2 using only the 3 regiusters. R1 and R2 should hold the values to be XOR'd and R0 should store the result. How do I do this?
r/lc3 • u/Fine-Nectarine544 • Oct 27 '21
Can anyone help me with my hw?
Using the LC-3 simulator, you will construct an assembly-level program that prompts the user for a starting address (in hex) and an ending address (in hex). Your program will then output the contents of memory (in hex) between the provided ranges (inclusive). An example execution of the memory dump routine might look like this:
Enter starting memory address:
x3000
Enter ending memory address:
x3001
Memory contents x3000 to x3001:
x3000 xF030
x3001 xF025
I/O for this routine requires that you develop a routine to enable the input of a 4-digit hex value and a routine for displaying the contents of a 16-bit registers/memory location as a 4-digit hex value. You should implement EACH of these two functions as separate user TRAPs according to these specifications:
Input (Trap x40): A Trap routine (invoked as TRAP x40) that reads a 4-digit hex from the keyboard and returns the value in R0. This trap may call other traps. You will develop this trap routine and locate it in memory at address x4000.
Output (Trap x41): A Trap routine (invoked as TRAP x41) that displays the contents of R0 to the display as a 4-digit hex value. This routine should output exactly 5 characters: a leading “x” and the 4 hex digits. Do not display a carriage return/line feed/end-of-line as part of the trap call. This trap may also call other traps. You will develop this trap routine and locate it in memory at address x5000.
Develop these TRAP routines and the use them to implement a program to perform memory dumps (using the i/o format provided in the example above).
Note: The representation for output characters (ASCII) is different than the standard binary representation of the value. For example, you may find it useful to note that the ASCII representation for any single-digit value is #48 greater than the number itself. Thus, the ASCII representation of the character 0 has value #48 (x30) while the ASCII representation of the character 1 has value #49 (x31).
An assembler for the LC-3 (in Python)
https://github.com/pepaslabs/lc3as.py
Could have sworn I posted this a little while ago, but I logged back in and didn't see my post, so here it is (again?).
I wrote an assembler for the LC-3, in Python. It isn't as hard as you might think!
First, you need to know how to write a parser. The best way to get started here is to follow Gary Bernhardt's approach of using a regex-based lexer and a recursive-descent parser: https://www.destroyallsoftware.com/screencasts/catalog/a-compiler-from-scratch
Next, just follow the description of how an assembler works in chapter 8 of the LC-3 book! Really, it is simpler than you think! The only trick is that you don't know the addresses of all the labels ahead of time, so you have to perform two passes: in the first pass, you calculate the labels of all of the addresses and store them in a "symbol table". This is easy for the LC-3, because all of the instructions are the same size! Then you perform the assembly pass, using your symbol table any time you encounter a label.
Cheers!
r/lc3 • u/[deleted] • Dec 12 '18
How to go about making an if statement in LC3?
I'm trying to figure out how to prompt the user to type in an opcode and then display the corresponding binary opcode. The strategy was to use an if statement but I'm not really sure how to implement that with the given LC3 instructions. Any ideas? I'd really appreciate it!
r/lc3 • u/ijustlovemath • Dec 11 '13
Video Series update
Hi all,
Due to a lack of feedback and a large amount of work to get the videos up to par, I never did end up making them. I've gotten some recent interest in this community from others and hope to make this series by the new year. With final exams running right now, I will not be free to start the project until late next week. Hopefully that's soon enough to help those of you who need it.
Until then, I'd definitely recommend reading a bit of Patt and Patel's Introduction to Computing Systems. It's a great resource on LC3.
Happy coding!
r/lc3 • u/ijustlovemath • Mar 22 '13
LC3 Tutorial thread
Hey everyone! By request, I'm putting together a series of videos explaining different aspects of the LC3 language and its implementations. To save time, I've sought out several preliminary lectures that will give you an understanding of the 2's complement data type and a few other important details that are critical to understanding the LC3 and its functions. I'll be sure to update this post with links as I get to them.
What is the 2's complement data type?
Episode List
Memory and Registers (Coming May 20th)
Address Space and Addressability
Labels and Assembler caveats
"NOT" in LC3
"AND" in LC3
"ADD" in LC3, please make sure you understand 2's complement before starting this one!
"LD" in LC3
"LDI" in LC3
"LDR" in LC3
LD vs LDI vs LDR (static vs dynamic operations)
"ST" in LC3
"STI" in LC3
"STR" in LC3
ST vs STI vs STR
BR and condition codes
TRAP vectors
JSR and RET for subroutines
JSRR and RET for subroutines
Long episode: Creating a multiplication subroutine
Final episode: Deconstructing the matrix multiplication code
Again, please post requests in the comments if you have any. I'm balancing a full courseload with this project, but hope to have it done soon, as the videos will be short and sweet.
Thanks guys!
r/lc3 • u/ijustlovemath • Mar 05 '13
How interested would you be in a matrix multiplication program I'm working on?
It takes input matrices A and B at x4100 and x4200 respectively, uses a value n stored at x4000, and computes the "n x n" matrix product and stores the result starting at x4300. It's got a fairly complex loop structure, so I'd have to update my comments, but I think you guys would appreciate it!
r/lc3 • u/Moegg123 • May 08 '23
HELP PLEASE!!!!
is anyone able to help me with my LC-3 assembly code, I am not sure why I have an infinite loop an am very unsure how to fix it. Please help me!
r/lc3 • u/LuucMeldgaard • May 06 '23
Rewriting short machine code to LC3 assembly code - question about LD operation
So, I have some machine code that I want to rewrite into LC3 assembly language.
I have tried to translate it, but I ahve run into a problem with what needs to be at x3007.
It looks to me, as if the number 10, would be stored at x3007, but it could also be, that the operation BR #10 is at that specific location. When I run the program using BR, the program seems to be adding a random number to R1, which makes the program do some weird behavior.
But when I load it with: NUM1 .FILL #10, it adds all numbers between 1-10 to R0, which equals 55, and halts the program at trap x25.
So, could it simply be, that the value 10 is stored at x3007 or am I simply doing something wrong.
Here you ahve the machine code, and the translation I have made:
______________________________________________________________________
.ORIG x3000 ; Sets the origin address of the program to x3000
AND R0, R0, #0
LD R1, #5 ;loads value at x3006
BRnz #3 ; sets PC at x3005
ADD R0, R0, R1
ADD R1, R1, #-1
BRnzp #-4
TRAP x25
NUM1 .FILL #10 ; OR BR #10
HALT
______________________________
X3000 0101 000 000 1 00 000
X3001 0010 001 000000101
X3002 0000 110 000 000011
X3003 0001 000 000 0 00 001
X3004 0001 001 001 1 11111
X3005 0000 111 111111100
X3006 1111 0000 00100101
X3007 0000 000 000001010
______________________________
.END
https://zserge.com/posts/post-apocalyptic-programming/
https://github.com/zserge/lc3-forth
https://zserge.com/posts/post-apocalyptic-programming/
A minimal Forth VM written in LC3 assembly to demonstrate bootstraping Forth from scratch
r/lc3 • u/labib0910 • Jul 19 '20
LC3!!!
Could anyone help me in drawing a 3*3 box using LC3 commands? I need help asap lol
r/lc3 • u/[deleted] • Apr 20 '17
LC-3 assembly language Help!!
Hello, I am new to this and am needing help with Asking user for number input including spacing, printing out what user has entered, enter key goes onto next step. Count of how many numbers where entered, smallest number, biggest number and numbers in order. No negative numbers can be entered.
r/lc3 • u/WUTDO11231235 • Dec 11 '16
ECE 120 LC3 Microarchitecture Table Solution?
Does anyone have the solution to THIS table from the ECE 120 book? It says in the book that the instructor will provide it, but mine said he didn't have it. I also couldn't find anything online. Do you know where I can find it? Here is what it says in the book:
Figure C.9 completes our microprogrammed implementation of the LC-3. It shows the contents of each location of the control store, corresponding to the 49 control signals required by each state of the state machine. We have left the exact entries blank to allow you, the reader, the joy of filling in the required signals yourself. The solution is available from your instructor.
r/lc3 • u/[deleted] • Feb 29 '16
lc3 edit download for windows? simulator was easy to find/download
it would be nice to be able to use LC3 Edit on my personal laptop
r/lc3 • u/Maxiplexi • May 04 '13
.FILL pseudo-op displayed as JMP in simulator
Hello!
I'm working with the LC-3 as part of a university assignment, and I discovered something about the LC-3 simulator (included with the Patt & Patel textbook) that I don't quite understand.
Whenever I write .FILL as part of my program, it seems to come up as JMP in the simulator. It's actually causing me a bit of grief with my assignment, because instead of simply storing a value I specify at a memory address, the .FILL command causes the program to JMP to an address stored in some register.
Do you have any idea why this might be?
Thanks for any help in advance :)
r/lc3 • u/ijustlovemath • Mar 09 '13
I'm considering posting a video series explaining how the lc3 works, where each video is 5 minutes or less. Comment for interest and/or requests
r/lc3 • u/ijustlovemath • Feb 18 '13
Welcome to /r/LC3! Feel free to ask questions about this subreddit in this thread, and I'll do my best to answer them.
r/lc3 • u/Excellent_Carpet_557 • Oct 29 '24
LC3 returning Japanese
I am not sure where I went wrong but my code is returning japanese when I try to run a xor function between two numbers, here is the code.
;
;
;
;
; [my name]
.ORIG x3000;
;
;
AND r0, r0, #0 ;CLEAR REGISTERS
AND r1, r1, #0
AND r2, r2, #0
AND r3, r3, #0
AND r4, r4, #0
AND r5, r5, #0
lEA r0, FIRSTSTG
PUTS;READ AND ECHO
GETC
OUT
ST R0, BIN1 ;SAVE FIRST SET
LEA r0, SECONDSTG
PUTS
GETC
OUT
ST r0, BIN2 ;SAVE SECOND SET;CONVERT IPUTS AND PREFORM XOR
LD r4, NEGASCII ;LOAD FIRST, CONVERT AND STORE
LD r0, BIN1
ADD r0, r0, r4
ST R0, BIN1
LD r1, BIN2
ADD r1, r1, r4
ST r1, BIN2; XOR FUNTION
NOT R2, R1 AND R2, R2, R0
NOT R4, R0
AND R4, R4, R1
ADD R3, R2, R4
; Prepare Result for Output
LD R4, ASCIIOFF
ADD R0, R3, R4 ;Convert result to ASCII
LEA R0, RESULTSTG ;Display Result
PUTS
OUT
LEA R0, BYESTG ; Goodbye Message
PUTS ; Print it
HALT
; Data Section
FIRSTSTG .STRINGZ "\nEnter First Number: "
SECONDSTG .STRINGZ "\nEnter Second Number: "
RESULTSTG .STRINGZ "\nThe XOR of the two numbers is: "
BYESTG .STRINGZ "\n\nThank you for playing!"
BIN1 .FILL x0000 ; Placeholder for first input
BIN2 .FILL x0000 ; Placeholder for second input
NEGASCII .FILL xFFD0 ; -x30, to convert ASCII to binary
ASCIIOFF .FILL x30 ; +x30, to convert binary to ASCII
.END
r/lc3 • u/honadawn1287 • Apr 11 '22
displaying a three digits number as a sum?
can anyone help me with displaying on the screen 3 digits and above number which is a result of a sum(for example I take a total of 81+82 and I want to display 163)
r/lc3 • u/Yimerwolley27 • May 11 '21
Plsplspls help , in need within 45 min!
Write an LC 3 assembly language program to check how many odd numbers are within 10 integer numbers. Assume: 1) the 10 integer numbers have been stored at memory locations x4100 ~ x4109; 2) the program starts at x3000; and 3) count number will be stored at memory location x5100.
r/lc3 • u/Mdrlolxd1998 • Jan 14 '21
Can anybody help me with this please?
Write an LC-3 assembler program that reverses a string of characters stored at the address str1 in a new string of characters at the address str2 (the first character of str1 becomes the last character of str2, the second character becomes the second last, etc.) and displays the result. The code will have to contain the assembler directives that reserve space to memorise both strings and initiate str1 with your dob.