Converting Problem

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
sevobal
Member
Member
Posts: 63
Joined: Sun Oct 22, 2006 7:11 am

Converting Problem

Post by sevobal »

Hi,
I've a little problem by converting a number to an ascii value. First of all I read the current date and time and save it:

Code: Select all

get_dtime:
		mov ah, 0x02
		int 0x1A
		
		mov byte [hour], ch
		mov byte [minute], cl
		mov byte [second], dh
		
		mov ah, 0x04
		int 0x1A
		
		mov byte [century], ch
		mov byte [year], cl
		mov byte [month], dh
		mov byte [day], dl
		
		ret
so far....The documentation says that all is stored in BCD format (ie. 23 decimal is stored 23h) and I can verify this. Now I try to convert the values (day, month, year and so on) to ascii values:

Code: Select all

convert_dtime:		
		mov ax, [day]
		mov bl, 0x10
		div bl

		add al, 0x30
                                add ah, 0x30
		
		mov byte [day_1], al
		mov byte [day_2], ah
		
		
		mov ax, [month]
		mov bl, 0x10
		div bl
		
                                add ah, 0x30
                                add al, 0x30
		
		mov byte [month_1], al
		mov byte [month_2], ah
				
		ret
and then I print it on the screen and I should get this one:
04.01
but instead of this I'm getting
04.p1
What I'm doing wrong? Where is my mistake? I'm not able to find it :(

THX
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Converting Problem

Post by Brendan »

Hi,
sevobal wrote:What I'm doing wrong? Where is my mistake? I'm not able to find it :(
Let's start with your first instruction, "mov ax, [day]". That loads 8-bits of trash in AH and a pair of 4-bit values in AL. Then you divide AX by 16 to get a messed up mixture of trash. Finally you convert this messed up mixture of trash into ASCII...

To convert a byte of BCD into ASCII, try something like:

Code: Select all

    mov al, [the_BCD_byte]  ;al = both BCD digits
    mov ah, al              ;ah = both BCD digits
    shr al, 4               ;al = first BCD digit (0 to 9)
    and ah, 0x0F            ;ah = second BCD digit (0 to 9)
    add ax, 0x3030          ;al = first digit in ASCII, ah = second digit in ASCII
A faster method is to use a lookup table. It costs 512 bytes (or 320 bytes if you know "out-of-range" values are impossible):

Code: Select all

   section .data
BCDtoASCII_lookupTable:
   db '00','01','02','03','04','05','06','07','08','09','??','??','??','??','??','??'
   db '10','11','12','13','14','15','16','17','18','19','??','??','??','??','??','??'
   <more here!>
   db '90','91','92','93','94','95','96','97','98','99','??','??','??','??','??','??'
   db '??','??','??','??','??','??','??','??','??','??','??','??','??','??','??','??'
   db '??','??','??','??','??','??','??','??','??','??','??','??','??','??','??','??'
   db '??','??','??','??','??','??','??','??','??','??','??','??','??','??','??','??'
   db '??','??','??','??','??','??','??','??','??','??','??','??','??','??','??','??'
   db '??','??','??','??','??','??','??','??','??','??','??','??','??','??','??','??'
   db '??','??','??','??','??','??','??','??','??','??','??','??','??','??','??','??'
   section .text

   movzx eax, byte [day]
   movzx ebx, byte [month]
   mov ax, [BCDtoASCII_lookupTable + eax]
   mov bx, [BCDtoASCII_lookupTable + ebx]
   mov [day_1], al
   mov [day_2], ah
   mov [month_1], bl
   mov [month_2], bh

Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
Post Reply