• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Converting decimal numbers to hex (assembly)

i'm writing a routine in assembly (using pascal) where i print out a decimal number, for part of a project involving decimal numbers. since the print character routine i'm using utilizes hex numbers only to print their ascii equivalents, i'm having some difficulty actually writing this.

i'm not exactly sure how i would go about this, so far all i can handle is printing single digit numbers by adding 30hex to them, to get the ascii number, which is in hex.

any ideas on how to do this?
 
Originally posted by: notfred
Since when is pascal an assembly language? 😕

you can write in assembly within pascal (it has a built in assembler), we use it because its easier than debug (although pretty much anything is easier than debug).
 
Originally posted by: DaveSimmons
mod 16 is your friend for bytes

for larger values, use a loop with mod 16 then / 16.

mod in assembly? i think you have to keep shifting bits and then ANDing the value w/ a mask to strip off the next multiple of 16. i did similar stuff about 4 years ago in cs, can't quite remember right now.
 
grr....
what i'm trying to do is divide the number i have by 10, but pascal keeps giving me this "divide by zero" error. i use the DIV command which assumes the number you want to divide is in AX, and then you type DIV [number]. well first of all, i cant just go ahead and do DIV 10, because pascal gives me a "invalid combination of opcode and operands" error. so i put 10 into CL, and run DIV CL. then the program runs, but when its at the point where the actual division would occur, i get the divide by zero error. any clues as to what i'm doing wrong here?
 
ok umm what is the input integer in? just integer or a string?

if it's just an integer, "and" it with a mask eg F to get the lowest digit, then convert it to its asci equiv which is easy
then shift it to the right by 4 bits, and again and repeat etc
 
erk, i'm not really getting what you're saying. i'm trying to stick with basic div since i'm a relative noob to assembly. basically what i have is this:

MOV BL, 0
MOV AX, DX
MOV CL, 10
@A: DIV CL
PUSH AX
INC BL
CMP AL, 16
JAE @A
@B: POP AX
MOV DL, AH
ADD DL, 48
CALL WriteChar

so, i make BL 0 (as the counter), then i move dx into ax, which is where the dividend needs to be. i move 10 into CL, which is what i'm dividing by. i divide, then push AX (to store the answer so i can save and print each digit in order) i keep looping until the quotient is less than 16. then i print in order.
it looks like it should work, but it keeps giving me the divide by zero error at DIV!
 
in pascal, 10 is decimal. unless you have a trailing h. so in this case, its 10 decimal. althought making it 10h still gives me the divide by zero error.
 
i actually had to do this in assembly, luckily the 68hc11 gives the quotient and remainder in the accumulators, so no need for a div and mod 😉
 
Originally posted by: dighn
err i hate reading assembly code. what exactly are you trying to achieve here?

i'm trying to extract each digit of the number by dividing by 10. the remainder is that particular digit. then you divide again by 10, the new remainder is the next digit, and so on. so, you print all the remainders in reverse order by adding 48(decimal) to each digit and printing the ascii code in hex, and you get your number printed in decimal.
 
Originally posted by: wfbberzerker
Originally posted by: dighn
err i hate reading assembly code. what exactly are you trying to achieve here?

i'm trying to extract each digit of the number by dividing by 10. the remainder is that particular digit. then you divide again by 10, the new remainder is the next digit, and so on. so, you print all the remainders in reverse order by adding 48(decimal) to each digit and printing the ascii code in hex, and you get your number printed in decimal.

from the thread title i was under the impression that you were trying to convert a decimal number to hex for printing... hmm so ur trying to print out decimal?

actually i'm totally confused by what you are trying to do lemmie read a bit more
 
Originally posted by: dighn
Originally posted by: wfbberzerker
Originally posted by: dighn
err i hate reading assembly code. what exactly are you trying to achieve here?

i'm trying to extract each digit of the number by dividing by 10. the remainder is that particular digit. then you divide again by 10, the new remainder is the next digit, and so on. so, you print all the remainders in reverse order by adding 48(decimal) to each digit and printing the ascii code in hex, and you get your number printed in decimal.

from the thread title i was under the impression that you were trying to convert a decimal number to hex for printing... hmm so ur trying to print out decimal?

basically, yes. sorry for the semi-misleading title. i'm trying to print out the decimal number, but in order to do that, i have to print it out it's ascii equivalent, which uses hex. so, i have to separate each digit, and then print out it's ascii character.
 
Originally posted by: wfbberzerker
Originally posted by: dighn
Originally posted by: wfbberzerker
Originally posted by: dighn
err i hate reading assembly code. what exactly are you trying to achieve here?

i'm trying to extract each digit of the number by dividing by 10. the remainder is that particular digit. then you divide again by 10, the new remainder is the next digit, and so on. so, you print all the remainders in reverse order by adding 48(decimal) to each digit and printing the ascii code in hex, and you get your number printed in decimal.

from the thread title i was under the impression that you were trying to convert a decimal number to hex for printing... hmm so ur trying to print out decimal?

basically, yes. sorry for the semi-misleading title. i'm trying to print out the decimal number, but in order to do that, i have to print it out it's ascii equivalent, which uses hex. so, i have to separate each digit, and then print out it's ascii character.

ohh ic you mean the ascii equivalent numerical value. it's the "uses hex" part that confused me.

ok i'll check it out
 
Originally posted by: wfbberzerker
erk, i'm not really getting what you're saying. i'm trying to stick with basic div since i'm a relative noob to assembly. basically what i have is this:

MOV BL, 0
MOV AX, DX
MOV CL, 10
@A: DIV CL
PUSH AX
INC BL
CMP AL, 16
JAE @A
@B: POP AX
MOV DL, AH
ADD DL, 48
CALL WriteChar

so, i make BL 0 (as the counter), then i move dx into ax, which is where the dividend needs to be. i move 10 into CL, which is what i'm dividing by. i divide, then push AX (to store the answer so i can save and print each digit in order) i keep looping until the quotient is less than 16. then i print in order.
it looks like it should work, but it keeps giving me the divide by zero error at DIV!

well i have o idea why you are getting dividision by 0, maybe u should debug it to see where is CL set to 0

btw why are you comparing al to 16? shoudn't you compare it to 10
 
Originally posted by: dighn
Originally posted by: wfbberzerker
erk, i'm not really getting what you're saying. i'm trying to stick with basic div since i'm a relative noob to assembly. basically what i have is this:

MOV BL, 0
MOV AX, DX
MOV CL, 10
@A: DIV CL
PUSH AX
INC BL
CMP AL, 16
JAE @A
@B: POP AX
MOV DL, AH
ADD DL, 48
CALL WriteChar

so, i make BL 0 (as the counter), then i move dx into ax, which is where the dividend needs to be. i move 10 into CL, which is what i'm dividing by. i divide, then push AX (to store the answer so i can save and print each digit in order) i keep looping until the quotient is less than 16. then i print in order.
it looks like it should work, but it keeps giving me the divide by zero error at DIV!

well i have o idea why you are getting dividision by 0, maybe u should debug it to see where is CL set to 0

btw why are you comparing al to 16? shoudn't you compare it to 10

yah, that was mistake, but i'm not even getting that far 🙂
 
i added a line to print the value of CL (in hex) right before the division, and it is indeed 10 (0A, actually). looking back at some of my old projects, they were written with the division in the same manner, with no problems.


actually, now that i think about it, i'm using a different version of turbo pascal than we had at school, could that have caused this problem? the version i'm using now is turbo pascal 7.0, and at school we used 6.6, i believe.
 
Back
Top