Message board for the users of flat assembler.
> DOS > Printing Decimal numbers??
I'm trying to create a program to print in the screen a number in decimal base from hexadecimal. I think my code is ok, but it doesn't work at all:
MOV CX,0x0000 MOV DL,0x0A MOV AL,0xFF Pervi: MOV AH,0x00 DIV DL INC CX PUSH AX CMP AL,0x0A JGE Pervi MOV AH,0x0E INT 0x10 Vtori: POP BX MOV AL,BH ADD AL,0x30 INT 0x10 DEC CX CMP CL,0x00 JZ Vtori HLT
The number I want to print is saved in AX, "00FF". While the quotient is greater or equal to 0A, I push the remainder into the stack. When this loop ends, I print the Quotient, and I start to pop and print the remainders from the stack.
I don't know exactly where is the problem, it may be syntax, but i don't think so. Please help
|06 Aug 2010, 08:44||
Don't have time to look right now, have to go to work. But here's a working one.
; os_int_to_string -- Convert unsigned integer to string
; IN: AX = int
; OUT: AX = string location
mov ecx, 0
mov ebx, 10 ; Set BX 10, for division and mod
mov edi, ITS ; Get our pointer ready
mov edx, 0
div ebx ; Remainder in DX, quotient in AX
inc ecx ; Increase pop loop counter
push dx ; Push remainder, so as to reverse order when popping
test eax, eax ; Is quotient zero?
jnz @b ; If not, loop again
pop dx ; Pop off values in reverse order, and add 48 to make them digits
add dl, '0' ; And save them in the string, increasing the pointer each time
mov [edi], dl
mov byte [edi], 0 ; Zero-terminate string
mov eax, ITS ; Return location of string
ITS db 11 dup (0)
It doesn't print the string, but returns the address to a 0 terminated string of the number in ascii, ready for printing.
|06 Aug 2010, 14:43||
Here's a simple routine that prints what's in AX. You can just zero out AH before you call it if you only want to print AL.
printax: pusha push 0 mov bx,10 @@: xor dx,dx div bx add dx,'0' push dx or al,al jz @f jmp @b @@: pop ax or al,al jz @f mov ah,0Eh int 10h jmp @b @@: popa ret
Hmmm, but I guess neither roboman's nor my post actually answers your question as to why yours doesn't work....
|06 Aug 2010, 15:54||
You missed an ADD AL, 0x30 before the first INT 0x10. Then, the CMP CX, 0 should be followed by a JNZ or JNE, not JZ or JE (you could also remove the CMP since DEC CX will set ZF to 1 when CX gets to zero).
This algorithm will fail when the number is less than 0x0A (the number will be prefixed with a zero while all the rest will not). Take a look at how you could fix it.
PS: I've assumed you want to print AL contents, otherwise, you need yet more fixes.
|06 Aug 2010, 16:35||
Thank you all for your alternative codes. In fact, the real problem was putting JNE instead or JE. Now my program works correctly.
|07 Aug 2010, 15:31||
You can also check out some of my conversion routines here...
|08 Aug 2010, 02:19||
base conversion is something very easy.
it is as easy as writing numbers on paper.
a digit is always equal to it's value multiplied by its rank.
then, 90543 = 9 * 10^4 + 0 * 10^3 + 5 * 10^2 + 4 * 10^1 + 3* 10^0
it is exactlly the same for base 2 (binary), base 8 (octal), base 10 (decimal), base 16 (hexadecimal), base 256(ascii)....
then, always the same formula to apply:
digit = value * base^rank.
number = Sigma [0>max] (value * base^rank).
then, i just propose one thing, add a moderated snippet section on the forum, something only edited by admins, something that need comments to be validated, and something non redondant, we don't need the mega supra algo from edfed or else, just the simple algo easy to understand.
it is a little hard to imagine such a ting, but is every sections of the forum have a >snippets section, it will be good. of course, it will be a post that will contain only snippets for the category of the forum, then, DOS forum will have dos snippets, etc...
i believe that it is a lack, and that it can be relativelly easy to implement.
then, i return to the reading of the forum.
|11 Aug 2010, 13:30||
< Last Thread | Next Thread >
Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.
Website powered by rwasa.