Write a LC-3 assembly language program to compute + - * / of complex numbers input form keyboard with the requirement below: number/ real parts / imaginary parts are signed integers give all prompts b4 inputting. outputting / 16 bits faceing to zero /mistakes.

2
Contributors
5
Replies
9
Views
5 Years
Discussion Span
Last Post by pyTony

Post your code for real numbers, and we will see if we can help to improve it.

This is the program to calculate +, -, *, / real numbers only. I donot know how to calculate complex number. please help me with this problem.

.ORIG x3000

;; strings can be stored at the start of the program
;; because their top 8 bits are not used.
szMessage .STRINGZ "Lets do some 16-bit integer math!\n+ add, - subtract, * multiply, / divide, x quit\n"
szError .STRINGZ "\nCannot divide by Zero\n"
szSizeError .STRINGZ "\nExceeds range (-32,768 <= x <= 32,767)\n"

LEA R0, szMessage
PUTS ; greet the user
START
LD R0, asciicrt
OUT ; print out a prompt of ">"
AND R1, R1, 0 ; \
AND R3, R3, 0 ; clean these
AND R4, R4, 0 ; /

; create the linked stack pointer
INLOOP
GETC ; loop grabbing input

;; enter denotes the end of input
LD R7, nasciinl
BRZ CALCULATE

;; type X to quit
LD R7, nasciix
BRZ QUIT

;; check to see if it's a digit
LD R7, nascii9
ADD R7, R0, R7 ; gotta be less than 9
BRP NAN
LD R7, nascii0
ADD R2, R0, R7 ; and greater than 0
BRN NAN
;; it's a digit, so we'll echo it. It's in R2 now.
OUT

; time to tally up the number we're recording
BRZ SKIPSH ; lets not waste time on the first digit
;; shift over a digit
AND R3, R3, 0
AND R7, R7, 0
SHIFT ; this loop multiplies the previous digits by 10
BRN TOOLARGE ; don't let the user type in something too big to record
BRP SHIFT
SKIPSH
ADD R1, R1, R2 ; add the new digit to our recording number
JSR INLOOP

;-----------------------------------------------------------------

NAN ;; it's not a number
AND R3, R3, 0 ; set up R3 for the next function
LD R7, nasciimul ; is it multiplication?
BRZ qMUL
LD R7, nasciidiv ; no, division?
BRZ qDIV
LD R7, nasciisub ; nope, subtraction?
BRZ qSUB
;; it's not an operation, so lets ignore it and go back to input.
JSR INLOOP

;---------------------------------------------------------------

;; lets assemble an opcode based on which operation it is
OUT
;; store it and the number in a new node on the linked stack
STR R1, R6, 0 ; store number in slot 1
AND R1, R1, 0 ; and clear it
STR R3, R6, 1 ; store operator in slot 2
ADD R6, R6, 3 ; increment the stack
STR R6, R6, -1 ; and provide a link to it in slot 3
;; and then back to input
JSR INLOOP

;---------------------------------------------------------------

DIV ;code 3
BRZ DIVERROR ; cannot divide by zero

AND R3, R3, 0
ADD R3, R3, -1 ; start a counter for the quotient
NOT R2, R2
DIVLOOP ADD R3, R3, 1 ; loop subtracting to divide
BRZP DIVLOOP
JSR SPUSH ; signed push
JSR MULDIV
DIVERROR
LEA R0, szError
PUTS
JSR START

MUL ;code 4
AND R3, R3, 0
BRN TOOLARGE ; don't let the product get too big to record
BRNP MULLOOP
JSR SPUSH ; signed push
TOOLARGE
LEA R0, szSizeError
PUTS
JSR START

SPUSH ;; by now, R3 contains the result, and R4 is the sign switch
ADD R4, R4, 0 ; check the signflag--
BRZ PUSH ; if it's set
NOT R3, R3 ; toggle the result's sign

PUSH ;; condenses the nodes in our linked stack. R5 should still contain the link
STR R3, R6, 0 ; store the sum
LDR R1, R5, 1 ; and lets squeeze in
STR R1, R6, 1 ; the next opcode.
LDR R5, R5, 2 ; R5 = R6->next->next;
STR R5, R6, 2 ; our condensed node will use the merging node's link
RET

;------------------------------------------------------------------------

LOADOPS ;; load operands for division and multiplication, and make them POSITIVE
AND R4, R4, 0 ; R4 is the sign flag: 0 = positive, -1/+1 = negative
LDR R1, R6, 0 ; load first operand
BRN LDOINV ; invert it and the sign flag if it's negative
LDR R5, R6, 2 ; get the link (keeping in !R5! for convenience)
LDR R2, R5, 0 ; and load the second operand from it
BRN LDOINV2 ; also invertable
RET
LDOINV
ADD R4, R4, -1 ; set inverted state
NOT R1, R1 ; invert R1
LDR R3, R6, 2
BRN LDOINV2 ; if negative...
RET
LDOINV2
ADD R4, R4, 1 ; either set or revert inverted state
NOT R2, R2 ; and invert R2
RET

;---------------------------------------------------------------

STR R1, R6, 0 ; add the last number to the stack
AND R0, R0, 0 ; and plug the opcode cell in it,
STR R0, R6, 1 ; so we know this is the end
LEA R6, linkedstack ; back to the start of the stack

MULDIV ;; level one: multiplication and division
LDR R0, R6, 1 ; get next op
BRZ ADDSUB ;; if the opcode is 0, we've hit the end of the list
;; now lets load our operands and operate
BRP MUL
BRZ DIV
;; lower precedence, skip it for now
LDR R6, R6, 2 ; R6 = R6->next
JSR MULDIV ; loop

LEA R6, linkedstack ; back to the top of the stack
ASLOOP
LDR R0, R6, 1 ; get next operation
BRZ PRINT

;; simple load operands, no sign switch
LDR R1, R6, 0 ; load first operand
LDR R5, R6, 2 ; get the link (keeping in !R5! as usual for convenience)
LDR R2, R5, 0 ; and load the second operand from it

;; check the opcode
;; if we're not adding we're subtracting, so make the second operand negative
NOT R2, R2
JSR SIGNMUX ; make sure it's a valid sum
JSR PUSH
JSR ASLOOP

;; the sum has passed the bounds if both operands have the same sign
;; and the sum has a different sign
SIGNMUX
ADD R1, R1, 0 ; check the first operand
BRN SMNEG
ADD R2, R2, 0 ; check the second operand
BRN CONDRET ; if it's not the same sign, the sum can't be invalid
ADD R3, R3, 0 ; check the sum
BRN TOOLARGE ; if +R1, +R2, and -R3, the sum is invalid
RET

SMNEG ADD R2, R2, 0 ; check second operand
BRZP CONDRET
ADD R3, R3, 0 ; and check the sum
BRZP TOOLARGE ; if -R1, -R2, and +R3, the sum is invalid
RET

PRINT
LD R0, asciieq ; print equals sign
OUT
LEA R0, buffer ; R0 is where we'll store the string
AND R4, R4, 0 ; leading zeroes switch, 0 if no digits recorded

LDR R1, R6, 0 ; R1 - number
BRZP POSITIVE
LD R2, asciimin ; stick on a '-' for negatives
STR R2, R0, 0
NOT R1, R1 ; and make em positive
POSITIVE
LD R3, ntenthou ; R3 - place subtractor
JSR TESTDIGIT

LD R3, nthou ; set thousands place
JSR TESTDIGIT

LD R3, nhund ; hundreds
JSR TESTDIGIT

AND R3, R3, 0
ADD R3, R3, -10 ; tens
JSR TESTDIGIT

AND R3, R3, 0
ADD R3, R3, -1 ; and ones
JSR TESTDIGIT

AND R1, R1, 0 ; and then plug the end of the string
STR R1, R0, 0
LEA R0, buffer ; back to the start of the string
PUTS
LD R0, asciinl
OUT
JSR START ; we're finished! Lets start again, yay.

;-----------------------------------------------------------

;; converts each decimal digit of the binary number into ascii
TESTDIGIT
LD R2, ascii0m1 ; R2 - counter

BRP TDLOOP ; we've got digits, so we'll record zeroes

ADD R5, R1, R3 ; R5 temp
BRN CONDRET ; no need to record this leading zero

TDLOOP ADD R2, R2, 1 ; increment
BRP TDLOOP
STR R2, R0, 0 ; store the digit
NOT R3, R3 ; fix last subtraction
ADD R3, R3, 1 ; ^
ADD R1, R1, R3 ; ^
ADD R4, R4, 1 ; we've recorded a digit, so zeroes can follow
RET

CONDRET ;; allows conditional BRs to return
RET

QUIT ;; jump here to end the program
HALT

; ascii codes to compare to
nascii0 .FILL -48
nascii9 .FILL -57
nasciinl .FILL -10
nasciisub .FILL -45
nasciidiv .FILL -47
nasciimul .FILL -42
nasciix .FILL -120
asciimin .FILL 45
asciispace .FILL 32
asciieq .FILL 61
asciinl .FILL 10
ascii0m1 .FILL 47
asciicrt .FILL 62

; decimal places for conversion
ntenthou .FILL -10000
nthou .FILL -1000
nhund .FILL -100

buffer .BLKW 6 ; string to store the answer

linkedstack .BLKW 92 ; this stack will hold 30 operations before overflowing.
stackend .FILL xBEEF ; puts BEEF at the end of the stack
; ...
.END

oh, I'm sorry, but I found it at http://www.sheezyart.com/art/view/255537/ and I donot know that it's your program.I think this program is better than my program, because my program cant compute negative number.

It is not mine, I got it from same place. My point was that it was not your effort, even you did state the source in your original post. It states clearly the source in my post.

Edited by pyTony

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.