0

I'm really new to PEP/8 and am having a bit of a hard time for a practice lab for my course. The goal is to ask user to input 2 hexadecimals to add or substract under the form HHHH+FFFF or HHHH-FFFF (no other input is accepted)

According to my teacher, I need to make my code read character by character out of the user input and then perform the calculation depending on the symbol in 5th position. How do I go about doing that?

Also, I cannot seem to find a way to add or substract hexadecimals directly, do I have to translate the HHHH and FFFF to decimal, then perform the calculation on the decimal numbers then translate back to hex for output?

Lastly, in my basic welcome message it is supposed to print out "Please input your calculation:" but the terminal always prints "Please input your calculation: A"...why does an A appear?

For the last part, my code is:

              Br     main 
hello_ms: .ASCII "Please input your calculation: "

main:  stro hello_ms, d 

stop
.end
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574

1 Answers1

2

Here is the answer to your last part - it appears that strings in the assembler language need to be manually NUL terminated:

hello_ms:   .ASCII "Please input your calculation:\x00" 

I have not coded for this assembler before, so I am learning as I read, but I've enough experience with other languages to know you will need to do the following:

To convert from ASCII char code to hex, you will need to compare if the char value is between '0' and '9', and if so, subtract '0' from it. If it is between 'A' and 'F', you need to subtract 'A' - 10 from it. This should get you a number from 0 to 15 eventually into your accumulator.

To add/subtract multiple digits, you will need to "shift" the values read up 8-places per hex digit into another register and then do the above step again per digit (4 times total) Beware that you may need to read the values in back to forward or shift 24/16/8 depending on what order you read your input.


Converting back follows the reverse steps.

Yes, I am not actually putting code here, but just giving you an overview on where to start.

The true meat of this assignment is the hex to decimal and decimal to hex conversion. Without this, you could just read in decimal directly with the appropriate opcode.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Michael Dorgan
  • 12,453
  • 3
  • 31
  • 61
  • Hey, I did that like you explained for the conversion part, it worked very well for 0 to 9, but I tried to apply the same logique for A to F and it does not work at all. If you could help me out I asked a separate question about it. https://stackoverflow.com/questions/44320115/assembly-pep8-converting-4-bit-hex-to-dec-issue-with-the-letter-parts – Oussama Arsenal BM Jun 02 '17 at 03:13
  • Notice I say 'A' - 10 - not just 'A' – Michael Dorgan Jun 02 '17 at 17:14
  • Zombie answer comes to life a few years later. Thanks for the updates :) – Michael Dorgan Apr 30 '19 at 17:30