flat assembler
Message board for the users of flat assembler.

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

Joined: 04 Dec 2003
Posts: 4
kingsz1 23 Mar 2004, 23:28
I set the hexadecimal value in register, e.g. AX = 3F, how I can display the value to screen in decimal number?
23 Mar 2004, 23:28

Joined: 04 Apr 2004
Posts: 10
Location: Hawaii
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. (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.
16 Apr 2004, 18:23
aaro

Joined: 21 Jun 2003
Posts: 107
Location: hel.fi
aaro 16 Apr 2004, 19:22
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
mov [esi], dword 0

.loop:
xor edx, edx
dec esi
div [Base]

cmp edx, 10
jb .digit

.digit:

mov [esi], dl
inc ecx
cmp eax, 0
jne .loop

push ecx

shr ecx, 2
rep movsd

pop ecx
lea eax, [.TmpBuf]
AStrAllocMac eax, ecx

popd esi, edi
return
endp
```
16 Apr 2004, 19:22
neonz

Joined: 02 Aug 2003
Posts: 62
Location: Latvia
neonz 17 Apr 2004, 09:56
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/.
17 Apr 2004, 09:56
slave17

Joined: 14 Mar 2004
Posts: 6
Location: Warsaw,Poland
slave17 20 Apr 2004, 17:03
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.
20 Apr 2004, 17:03
Matrix

Joined: 04 Sep 2004
Posts: 1166
Location: Overflow
Matrix 10 Sep 2004, 06:17
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

MATRIX
10 Sep 2004, 06:17
Matrix

Joined: 04 Sep 2004
Posts: 1166
Location: Overflow
Matrix 11 Sep 2004, 06:26
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
mov ah,0xE
int 10h
mov ax,dx
.down2:
mov dx,0
mov cx,1000
div cx
mov ah,0xE
int 10h
mov ax,dx
.down3:
mov cl,100
div cl
mov dl,ah
mov ah,0xE
int 10h
mov al,dl
mov ah,0
.down4:
mov cl,10
div cl
mov dl,ah
mov ah,0xE
int 10h
mov al,dl
.down5:
mov ah,0xE;
int 10h
ret

```

MATRIX
11 Sep 2004, 06:26
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

 Jump to: Select a forum Official----------------AssemblyPeripheria General----------------MainTutorials and ExamplesDOSWindowsLinuxUnixMenuetOS Specific----------------MacroinstructionsOS ConstructionIDE DevelopmentProjects and IdeasNon-x86 architecturesHigh Level LanguagesProgramming Language DesignCompiler Internals Other----------------FeedbackHeapTest Area

Forum Rules:
 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot vote in polls in this forumYou cannot attach files in this forumYou can download files in this forum