flat assembler
Message board for the users of flat assembler.

Index > DOS > Printing Decimal numbers??

Author
Thread Post new topic Reply to topic
Tovarisch



Joined: 06 Aug 2010
Posts: 3
Tovarisch
Hi!
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:
Code:
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 Confused

Thanks
Post 06 Aug 2010, 08:44
View user's profile Send private message Reply with quote
roboman



Joined: 03 Dec 2006
Posts: 122
Location: USA
roboman
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

os_int_to_string:
pusha

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
inc edi
dec ecx
jnz @b

mov byte [edi], 0 ; Zero-terminate string

popa
mov eax, ITS ; Return location of string
ret


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.
Post 06 Aug 2010, 14:43
View user's profile Send private message Visit poster's website Reply with quote
windwakr



Joined: 30 Jun 2004
Posts: 827
Location: Michigan, USA
windwakr
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.

Code:
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....
Post 06 Aug 2010, 15:54
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
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.
Post 06 Aug 2010, 16:35
View user's profile Send private message Reply with quote
Tovarisch



Joined: 06 Aug 2010
Posts: 3
Tovarisch
Thank you all for your alternative codes. In fact, the real problem was putting JNE instead or JE. Now my program works correctly.
Post 07 Aug 2010, 15:31
View user's profile Send private message Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 764
Location: Massachusetts, USA
bitshifter
You can also check out some of my conversion routines here...
http://board.flatassembler.net/topic.php?t=11615
Have fun Razz
Post 08 Aug 2010, 02:19
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4240
Location: 2018
edfed
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.
Post 11 Aug 2010, 13:30
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.