3 Replies - 5431 Views - Last Post: 31 August 2015 - 01:45 PM

#1 Zetsu   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 31-July 15

Using LC3 assembly I need to subtract two single digit numbers.

Posted 31 July 2015 - 01:42 PM

Instructions
I am supposed to prompts the user to input two numeric characters ('0'
... '9') from the user using Trap x20 (GETC). Make sure to echo the characters and store them into
different registers. The second number will be subtracted from the first, and the operation reported in
the console: <first number> ‐ <second number> = <difference> .
Output to the console the operation being performed (e.g. 4 ‐ 6 = ). Using the same registers, convert
the numeric characters into the actual numbers they represent (e.g.convert ‘2’ into 2).
Perform the subtraction operation, and determine the sign (+/‐) of the result; if negative, determine the
magnitude of the result (i.e. take 2's complement).
(Ex) Program performs (4‐6) and stores the result, ‐2, in a register. Program recognizes that result is
negative, converts ‐2 to 2, and sets flag for minus sign.
Convert resulting number back to a printable character and print it, together with minus sign if
necessary.
(Ex) Program converts 2 into '2', and stores it back in same register. Program outputs the two characters
‐2 followed by newline. thus completing the subtraction operation output.

My guess
I am not fully sure how I should begin
I think I'm supposed to start with TRAP x20 and then register it to something like R3 and then repeat for a second number for R4. Then I have to subtract I think with a combination of ADD and NOT

What I have so far
.ORIG x3000 ; Program begins here
;-------------
;Instructions
;-------------
;----------------------------------------------
;outputs prompt
;----------------------------------------------
LEA R0, intro ;
PUTS ; Invokes BIOS routine to output string
;-------------------------------
;INSERT CODE STARTING FROM HERE
;--------------------------------
HALT ; Stop execution of program
;------
;Data
;------
; String to explain what to input
intro .STRINGZ "ENTER two numbers (i.e '0'....'9')\n"
NEWLINE .STRINGZ "\n" ; String that holds the newline character
;---------------
;END of PROGRAM
;---------------
.END


This post has been edited by macosxnerd101: 31 July 2015 - 03:05 PM
Reason for edit:: Added code tags and moved to Assembly


Is This A Good Question/Topic? 0
  • +

Replies To: Using LC3 assembly I need to subtract two single digit numbers.

#2 turboscrew   User is offline

  • D.I.C Lover
  • member icon

Reputation: 168
  • View blog
  • Posts: 1,077
  • Joined: 03-April 12

Re: Using LC3 assembly I need to subtract two single digit numbers.

Posted 18 August 2015 - 02:42 AM

I don't know about the BIOS and its TRAPS, but about the rest of the stuff: yes.

This post has been edited by turboscrew: 18 August 2015 - 02:43 AM

Was This Post Helpful? 0
  • +
  • -

#3 NeoTifa   User is offline

  • NeoTifa Codebreaker, the Scourge of Devtester
  • member icon





Reputation: 4220
  • View blog
  • Posts: 18,507
  • Joined: 24-September 08

Re: Using LC3 assembly I need to subtract two single digit numbers.

Posted 19 August 2015 - 08:53 AM

I think your guess is correct. I haven't used LC3 in a minute, but that sounds about right.
Was This Post Helpful? 0
  • +
  • -

#4 turboscrew   User is offline

  • D.I.C Lover
  • member icon

Reputation: 168
  • View blog
  • Posts: 1,077
  • Joined: 03-April 12

Re: Using LC3 assembly I need to subtract two single digit numbers.

Posted 31 August 2015 - 01:45 PM

Oops, didn't realize - NOT is 1's complement.
You need to add 1 to make it 2's complement:
a - b = a + NOT( b ) + 1

You read a character with trap x20 (character emerges in low byte of r0).
You echo it with trap x21 (with character in low byte of r0)
The decimal digits are converted to binary subtracting '0' (0x30) from the ASCII-code, and vice versa. Multidigit numbers require a bit more.

It might be a good idea to write a pseudocode first.

And remember to decide which registers you use for what and where, and stick to the plan (unless you need to rethink that). Otherwise the usage of registers get blurred, and one ends up using wrong registers - like printing address instead of value, or incrementing data instead of counter.

This post has been edited by turboscrew: 31 August 2015 - 01:45 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1