user591272
user591272

Reputation:

decimal to hex in assembler

The system I'm working with, has four registers: al, bl, cl, dl and values can be stored only as hex.

I am reading from keyboard 2 digits, e.g. 9 and 1.

I would like to store this number in the BL register, as 5B which is the hex value for 91 (decimal).

I've been thinking of ways to solve this, but I can't find any. My main mistake was I was multiply 9 by 10, and add 1, result is 91, but I've forgot that actually 91 is in hex, which translated to decimal becomes 145 (which is not my number)

So, how do I store the two digits (9 and 1, which form 91) as hex value (5B) into some register or in RAM.

Please advise.

Many thanks, V

Upvotes: 1

Views: 10588

Answers (3)

Desu_Never_Lies
Desu_Never_Lies

Reputation: 690

If I understood it correctly, you was multiplying by 10h (which is 16 in dec), so it was producing wrong results.

   ;assuming val_1 is 9h and val_2 is 1h

   mov al, val_1
   mul al, 0Ah
   add al, val_2
   mov bl, al

Upvotes: 0

AusCBloke
AusCBloke

Reputation: 18532

The number isn't stored as hex, decimal or whatever, it's up to you to interpret it as such when you want to do something like creating a human-readable string of the value in the register.

The integer value 91 in decimal = 5B in hex = 1011011 in binary, it's all stored the same way in the register - as a binary value.

Since you're taking in input as decimal, then it's right for you to multiply 9 by 10 and then to add the 1 in order to end up with the decimal number 91 (or interpreted as hex - 5B).

Be careful if the input is the ASCII representation (or any other encoding) of the character '9', in which case you'll need to convert the code to the actual decimal value. If it is in fact an ASCII value then you'd just subtract the character '0' to get the decimal number 9.


When you want to interpret the binary value of the register as hex for perhaps printing, you'd build up the string by repeatedly getting the remainder of dividing the value in the register by 16, as outlined here. It's the same process for constructing a decimal string (divide by 10) or a binary string (divide by 2).

Upvotes: 3

Greg Hewgill
Greg Hewgill

Reputation: 994727

My main mistake was I was multiply 9 by 10, and add 1, result is 91, but I've forgot that actually 91 is in hex

That's not a mistake. As long as you are multiplying by 10 (decimal) and not 10h (hex, which is 16 decimal), then you'll get the answer you are looking for.

Values stored in CPU registers are just binary numbers, they aren't "in hex" or "in decimal".

Note that you may occasionally run into BCD, which aren't binary numbers but are another thing entirely.

Upvotes: 4

Related Questions