patternMinor
Implementing OR with the lc-3
Viewed 0 times
withimplementingthe
Problem
Today, something possessed me and I started learning assembly. My poison of choice is the lc-3, this is my very first program.
It is actually only the first part of a two part problem, but this already took enough that I thought it merited a review, just in case the day's insanity also expressed itself in my code.
It runs without errors, I'm fairly confident the OR logic is sound, I'd like to focus on ascertaining that I'm storing/loading effectively and in the correct locations.
The task:
Perform an OR on two binary numbers stored in X3020 and x3021
Store the result at x3022
My code:
Gratitude to the brave and wonderful souls who review assembly code.
It is actually only the first part of a two part problem, but this already took enough that I thought it merited a review, just in case the day's insanity also expressed itself in my code.
It runs without errors, I'm fairly confident the OR logic is sound, I'd like to focus on ascertaining that I'm storing/loading effectively and in the correct locations.
The task:
Perform an OR on two binary numbers stored in X3020 and x3021
Store the result at x3022
My code:
.ORIG x3000 ;
LD R0, x001f ; fetches data from x3020, stores into R0
LD R1, x001f ; fetches data from x3021, stores into R1
NOT R0, R0 ; start of OR logic
NOT R1, R1
AND R2, R0, R1
NOT R2, R2 ; end of OR logic
ST R2, x001B ; Stores result into x3022
HALT
.ENDGratitude to the brave and wonderful souls who review assembly code.
Solution
LD R0, x001f ; fetches data from x3020, stores into R0
LD R1, x001f ; fetches data from x3021, stores into R1
ST R2, x001B ; Stores result into x3022All of these instructions are correct. When the first instruction is being executed the PC is at x3001. Adding x001F to it will correctly address location x3020. When the third instruction is being executed the PC is at x3007. Adding x001B to it will address location x3022.
From the manual we learn
Program Counter; 16-bit register that contains the memory address of the next
instruction to be fetched. For example, during execution of the instruction at address
A, the PC contains address A + 1, indicating the next instruction is contained in
A + 1.
and also
Memory address space 16 bits, corresponding to 65,536 locations, each
containing one word (16 bits). Addresses are numbered from 0 (i.e, x0000)
to 65,535 (i.e., xFFFF).
Here's an alternative to your OR solution
NOT R1,R1
AND R0,R0,R1
NOT R1,R1
ADD R0,R0,R1The less registers you use the better on the very limited architecture of the LC-3.
Code Snippets
LD R0, x001f ; fetches data from x3020, stores into R0
LD R1, x001f ; fetches data from x3021, stores into R1
ST R2, x001B ; Stores result into x3022NOT R1,R1
AND R0,R0,R1
NOT R1,R1
ADD R0,R0,R1Context
StackExchange Code Review Q#85030, answer score: 2
Revisions (0)
No revisions yet.