flat assembler
Message board for the users of flat assembler.

Index > DOS > Print AX value

Author
Thread Post new topic Reply to topic
Masood.Sandking



Joined: 12 Jan 2012
Posts: 65
Location: Iran
Masood.Sandking
hi,
i use this code to print AX value, but it can't be greater than 99.

Code:
mov ax,55

mov bx,10       ;bx -> divide by 10
DIV Bl        ;divide. AL is quotient, AH is Remainder

add al,48       ;any number + 48 = the same number in ASCII
add ah, 48
push ax  ;push ax to the stack

mov ah,0x0e  ;print to screen
mov bx,0x0007
int 0x10

pop ax     ;pop ax back from the stack
mov al,ah        ;puts ah into al to print with int 0x10

mov ah,0x0e      ;print to screen
mov bx,0x0007
int 0x10
    

i am new to FASM. what is the best way?
Post 12 Jan 2012, 21:30
View user's profile Send private message Yahoo Messenger Reply with quote
avcaballero



Joined: 02 Feb 2004
Posts: 191
Location: Madrid - Spain
avcaballero
Hello, I wrote this code some time ago. Nasm sintax. Maybe it could help you.

Regards


Description:
Download
Filename: w2ascii1.zip
Filesize: 1.17 KB
Downloaded: 226 Time(s)

Post 13 Jan 2012, 07:25
View user's profile Send private message Visit poster's website Reply with quote
freecrac



Joined: 19 Oct 2011
Posts: 117
Location: Germany Hamburg
freecrac
Masood.Sandking wrote:
hi, i use this code to print AX value, but it can't be greater than 99.....

This is because you only divide by 10, but the maximum value in AX can be up to 65536, so you have to divide the value in AX at first by 10.000 to become at last 5 decimal ASCIIs(maybe the first are 0) and not only 2 ASCIIs.

Unsigned divide DX:AX by r/m word (AX=Quo, DX=Rem).

Dirk
Post 13 Jan 2012, 09:07
View user's profile Send private message Send e-mail Reply with quote
GhostXoPCorp



Joined: 13 Dec 2008
Posts: 199
Location: 01F0:0100
GhostXoPCorp
16 bits = 4 nibbles = FFFF = 0-65535 or a total of 65536 different numbers.


Division of the place value you your wanting to use, and putting the remainder into a buffer, and loop until ax = 0. Read up on how xor dx,dx is needed for word division and what cwd can do for you
Post 14 Jan 2012, 19:20
View user's profile Send private message Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 518
Location: Munich
shutdownall
You can use ax to easily carry values up to 9999.

Code:
mov ax,9999h

@@:
and al,0fh
add al,'0'
<print>
shr ax,4
jmp @b
    


This will give wrong order of digits but print all.
You can extend the code yourself, also calculating with bcd (binary coded decimals) is implemented in cpu and adjustment of digits after calculation.

Here is a tutorial
http://webster.cs.ucr.edu/AoA/Windows/HTML/AdvancedArithmetica6.html
Post 15 Jan 2012, 01:10
View user's profile Send private message Send e-mail Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
Just in case this is not obvious for the OP the code above has several problems:
1. It will print trailing zeroes forever (unless <print> takes care of jumping out of the loop)
2. Ignoring problem 1., it will print only half of the digits of the numbers correctly, the other digits will always be printed as zero.
3. Assumes the numbers are in packed BCD form.
4. As already stated by the author, it prints the digits in the wrong order.
Post 15 Jan 2012, 02:42
View user's profile Send private message Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 518
Location: Munich
shutdownall
LocoDelAssembly wrote:
Just in case this is not obvious for the OP the code above has several problems:
1. It will print trailing zeroes forever (unless <print> takes care of jumping out of the loop)
2. Ignoring problem 1., it will print only half of the digits of the numbers correctly, the other digits will always be printed as zero.
3. Assumes the numbers are in packed BCD form.
4. As already stated by the author, it prints the digits in the wrong order.


This is a quick and dirty example that ax can hold more than only the value 99 and print out. Masood.Sandking can find more information in the bcd tutorial and should read it. And with respect, a little bit of programming he can do himself. Wink


Here corrected version of example regarding point 2. I think some small errors will teach the stuff better than just give a full working example. Razz

Assume that bp is not used in the <print> routine. This version will work even with value 0 for ax. But not killing leading 0's.
Code:
mov ax,9999h
mov bp,4
@@:
push ax
and al,0fh
add al,'0'
<print>
pop ax
shr ax,4
dec bp
jnz @b
    
Post 15 Jan 2012, 12:31
View user's profile Send private message Send e-mail Reply with quote
Masood.Sandking



Joined: 12 Jan 2012
Posts: 65
Location: Iran
Masood.Sandking
Thanks to all!
my problem solved. here is the code:



Code:
mov ax,[sum]
mov bx,strsum

inttostr:
        ;In bx the offset to the target string
        ;In ax the unsigned integer to convert to string
        ;It converts an unsigned integer to an ascii string
        push ax
        push bx
        push cx
        push dx
        push si
        xor     cx,     cx
        ;xor     dx,     dx  THIS LINE IS BUGGY.
        mov     si,     10
label2:
        ;I divide the number by 10 repeatedly
        ; and I put the remainders in the stack.
        cmp     ax,     10
        jb      label1
        ;THIS IS THE PLACE FOR xor dx, dx
        ;CLEAR THE PREVIOUS REMAINDER.
        xor      dx,      dx
        div     si
        push    dx
        inc     cx
        jmp     label2

label1:
        push    ax
        inc     cx
label3:
        ;I retrive the numbers in the stack and
        ;I add to them the ascii value for '0' (character).
        pop     dx
        add     dx, '0'
        mov     byte [ds:bx], dl
        inc     bx
        dec     cx
        jnz     label3
        ;The service 9h of the 21h interrupt prints a string
        ;terminated by the character '$'
        mov     byte [ds:bx], '$'
    
Wink Wink
Post 18 Jan 2012, 09:51
View user's profile Send private message Yahoo Messenger Reply with quote
freecrac



Joined: 19 Oct 2011
Posts: 117
Location: Germany Hamburg
freecrac
Masood.Sandking wrote:

Code:
        ....
        mov     byte [ds:bx], dl
        ....
        ....
        ....
        mov     byte [ds:bx], '$'
    

Hint: For both instructions we donĀ“t need a segment override prefix for the DS-segment.

....

Here is an example to print EAX.
Code:
          ; print EAX
DECOUT:   mov     di, ASCII
          mov     cl, 0Ah             ; 10 decimal ASCII
          mov     ebx, 1000000000
P1:       xor     edx, edx
          div     ebx
          add     al, 30h
          mov     esi, edx
          mov     [di], al
          inc     di
          mov     eax, ebx
          mov     ebx, 0Ah
          xor     edx, edx
          div     ebx
          mov     ebx, eax
          mov     eax, esi
          dec     cl
          jnz P1
;-------
          mov     si, ASCII-1
          mov     dx, si
          inc     dx
ZERO:     inc     si
          cmp    BYTE[si], "0"
          jz  ZERO
          sub     si, 0Ah
          cmp     si, dx
          jz  short SKIP
          add     si, 0Ah
          mov     dx, si
SKIP:     mov     ah, 9
          int   21h
          ret

ASCII     DB "0000000000", "$"
    

Dirk


Last edited by freecrac on 19 Jan 2012, 08:19; edited 1 time in total
Post 19 Jan 2012, 07:55
View user's profile Send private message Send e-mail Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1288
Location: behind the arc
Picnic
Masood.Sandking see also this topic,

i wrote a Sample code for Outputting integers
Post 19 Jan 2012, 08:10
View user's profile Send private message Reply with quote
GhostXoPCorp



Joined: 13 Dec 2008
Posts: 199
Location: 01F0:0100
GhostXoPCorp
Bravo Freecrac, you managed to do that in 2/3 the amount of code it took me. Smile

_________________
Oh that divide overflow. Just jumps out of the bushes every time to scare the day lights out of me.
Post 21 Jan 2012, 08:38
View user's profile Send private message 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.