0

I am new to assembly and I'm not sure why my code isn't working.

I'm inputing and displaying in ASCII format, I can see the 1 and 3, but why isn't the addition sum showing?

My program is simply suppose to perform an addition. And display something like:

'>1+3=4

if the inputs are 1 and 3, then if user enters ! it stops, otherwise loops back.


Loop,Load    Greater
    Output      /for the ">" symbol

    Input 
    Output
    Store    X  /Taking input, storing and displaying 1st operand

    Load    Addr
    Output      /Loading and displaying "+"

    Input
    Output
    Store    Y  /Inp, str, displaying 2nd operand

    Load    Equal
    Output            /Display "="

    Load     X
    Add    Y
    Output       /Adding then displaying sum

    Input
    Store   Cond
    Skipcond     800
    Jump     Loop

    Halt

Greater,    DEC 62  
X,          DEC 0
Addr,       DEC 43
Y,        Dec 0
Equal,    Dec 61
Cond,      DEC 0```
Hello World
  • 191
  • 2
  • 9

1 Answers1

2

In order to get characters like !, you'll need to put the simulator into input mode accepting unicode characters instead of numbers (hex/decimal/binary).  And of course, in order to print characters like > and = you'll need to have the output configured for unicode as well.

Now that you're using ascii/unicode input & output, handling characters is the name of the game.  (Multi-digit numbers are character sequences.)

The ascii digit 1 has value 49, and 3 is 51.  You're adding 49 + 51 and getting 100, which is the ascii code for lower case d.  You need to subtract one ascii 0 (which is 48) and then you'll have 52, which is ascii 4.

If you wanted the numeric value, 4, you would have to subtract 0 (48) from both input digits.  And if you wanted to print the sum value as a digit in ascii, you'll need to add a 48 to that result.  In this case, that would be two subtractions of 48 and one addition of same — hence the short cut, subtract just one 48 and the digit will print.

Once you're in the mode of handling characters, though, if you want to print multiple digits — i.e. for a sum >= 10 — you'll need a real conversion from integer to ascii (and maybe vice versa too) so as to print as many digits as necessary.  (FYI, there's lots of examples of this in different languages, sometimes called itoa and atoi — for ascii to int and int to ascii respectively.)


To test for ! you'll need a subtraction of the ascii code for !, namely 33, followed by a SkipCond 000/Jump Loop sequence that tests for zero and skips the backward branch on zero (equal to !).

Erik Eidt
  • 23,049
  • 2
  • 29
  • 53
  • Thanks! So I put SkipCond 800 to test for !, since it's 33 in ASCII and is greater than 0, it should skip if ! is entered. My program is suppose to keep running if I press enter without nothing, but as I looked online, ENTER ASCII is 10, which is also more than 0, so I was expecting my program to not loop back as well if I pressed ENTER, but it actually does loop back. Do you know why? – Hello World Jun 14 '20 at 00:25
  • All ascii characters are > 0 (except the NUL character, which is very hard to type). You need to compare with something specific, rather than comparing against zero. To compare with something specific, do a subtraction before the `Skipcond`. – Erik Eidt Jun 14 '20 at 00:33
  • I checked what's in the AC after I pressed ENTER (without anything else), and it was actually 0, so I guess in this case I don't have to do the subtraction! – Hello World Jun 14 '20 at 01:29