flat assembler
Message board for the users of flat assembler.

Index > DOS > how can I display decimal number from a register hex number?

Author
Thread Post new topic Reply to topic
kingsz1



Joined: 04 Dec 2003
Posts: 4
kingsz1
I set the hexadecimal value in register, e.g. AX = 3F, how I can display the value to screen in decimal number?
Post 23 Mar 2004, 23:28
View user's profile Send private message Reply with quote
1800askgeek



Joined: 04 Apr 2004
Posts: 10
Location: Hawaii
1800askgeek
Well... if it is put in in hex so:

mov ax, 3fh

should be equivilent to:

mov ax, 63

unless I'm mistaken.

So, if you should be able to print it by figuring out the length, then in reverse order of the digits +48 to them and print then on screen w/ normal print commands. However, I haven't gotten it to work yet. Confused (I keep getting screwed up with the div stuff, like dividing the 63 by 10, moving the modulus, and subtracting the modulus from the original w/o loosing any data. The problem is I don't realy understand variables in assembly yet. Embarassed
Post 16 Apr 2004, 18:23
View user's profile Send private message Visit poster's website AIM Address Reply with quote
aaro



Joined: 21 Jun 2003
Posts: 107
Location: hel.fi
aaro
Here's proc that converts number to string with any base(decimal=10).
Code:
ASTR_UPPER = 'A'-'0'-10
ASTR_LOWER = 'a'-'0'-10
ASTR_SIGNED = 80000000h

proc AStrFromNum, Num, Base, Flags
type noframe
var .TmpBuf, 40
begin
     pushd edi, esi

  mov eax, [Num]
      lea edi, [.TmpBuf]
  xor ecx, ecx
        
    test eax, [Flags]
   jns .notneg
 neg eax
     mov [edi], byte '-'
       inc ecx
     inc edi
     
.notneg:
    mov esi, edi
        add esi, 32
 mov [esi], dword 0
  
.loop:
      xor edx, edx
        dec esi
     div [Base]

      cmp edx, 10
 jb .digit
   
    add dl, byte [Flags]
        
.digit:
     add edx, '0'
      
    mov [esi], dl
       inc ecx
     cmp eax, 0
  jne .loop

       push ecx
    
    add ecx, 3
  shr ecx, 2
  rep movsd
   
    pop ecx
     lea eax, [.TmpBuf]
  AStrAllocMac eax, ecx
       
    popd esi, edi
       return
endp
    
Post 16 Apr 2004, 19:22
View user's profile Send private message Reply with quote
neonz



Joined: 02 Aug 2003
Posts: 62
Location: Latvia
neonz
I can suggest following code:

Code:
;
; input:
;   al = single hex digit
;
; output:
;   al = single ASCII digit
;
; destroys:
;   flags
;

        cmp     al,10           ; if x < 10, set CF = 1
        sbb     al,69h          ; 0-9: 96h .. 9Fh,  A-F: A1h..A6h
        das                     ; 0-9: subtr. 66h -> 30h-39h,
                                ; A-F: subtr. 60h -> 41h..46h
    


I found it on http://www.brillianet.com/programming/assembly/source_code/.
Post 17 Apr 2004, 09:56
View user's profile Send private message Visit poster's website Reply with quote
slave17



Joined: 14 Mar 2004
Posts: 6
Location: Warsaw,Poland
slave17
i would suggest you use a simple hashtable if the numbers to print don't exceed a certain value. pay attention to the memory limits in real mode- even a table of 64k would be too much. if i was you id just modify the method above to make it a little bit faster.it also not stupid to combine these methods: take as base 100d, 1000d and make hashtables,copy the pointed string from the hashtable to a prepared location in mem and then print everything at once.this method is faster than just dividing by 10d and allows printing almost unlimited size numbers.
Post 20 Apr 2004, 17:03
View user's profile Send private message Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1171
Location: Overflow
Matrix
neonz wrote:
I can suggest following code:

Code:
;
; input:
;   al = single hex digit
;
; output:
;   al = single ASCII digit
;
; destroys:
;   flags
;

        cmp     al,10           ; if x < 10, set CF = 1
        sbb     al,69h          ; 0-9: 96h .. 9Fh,  A-F: A1h..A6h
        das                     ; 0-9: subtr. 66h -> 30h-39h,
                                ; A-F: subtr. 60h -> 41h..46h
    


I found it on http://www.brillianet.com/programming/assembly/source_code/.


Hello Neonz, its a nice snipplet, but he wanted to display decimal Smile

MATRIX
Post 10 Sep 2004, 06:17
View user's profile Send private message Visit poster's website Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1171
Location: Overflow
Matrix
i think i will help again

Code:

bwritewordu: ; writes AX to screen @ x,y (at cursor)
mov bx,7    ; returns:  AX,CX,DX = undefined ; BX = 7
cmp ax,10000
jnb .down1
cmp ax,1000
jnb .down2
cmp ax,100
jnb .down3
cmp ax,10
jnb .down4
jmp .down5
.down1:
mov dx,0
mov cx,10000
div cx
add al,48
mov ah,0xE
int 10h
mov ax,dx
.down2:
mov dx,0
mov cx,1000
div cx
add al,48
mov ah,0xE
int 10h
mov ax,dx
.down3:
mov cl,100
div cl
mov dl,ah
add al,48
mov ah,0xE
int 10h
mov al,dl
mov ah,0
.down4:
mov cl,10
div cl
mov dl,ah
add al,48
mov ah,0xE
int 10h
mov al,dl
.down5:
add al,48
mov ah,0xE;
int 10h
ret                   

    


MATRIX
Post 11 Sep 2004, 06:26
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.