flat assembler
Message board for the users of flat assembler.
Index
> DOS > Printing Decimal numbers?? |
Author |
|
roboman 06 Aug 2010, 14:43
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. |
|||
06 Aug 2010, 14:43 |
|
windwakr 06 Aug 2010, 15:54
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.... |
|||
06 Aug 2010, 15:54 |
|
LocoDelAssembly 06 Aug 2010, 16:35
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 |
|
Tovarisch 07 Aug 2010, 15:31
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 |
|
bitshifter 08 Aug 2010, 02:19
You can also check out some of my conversion routines here...
http://board.flatassembler.net/topic.php?t=11615 Have fun |
|||
08 Aug 2010, 02:19 |
|
edfed 11 Aug 2010, 13:30
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 > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.