OSDev.org

The Place to Start for Operating System Developers
It is currently Tue Mar 19, 2024 5:08 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 14 posts ] 
Author Message
 Post subject: Printing a BCD value
PostPosted: Wed May 04, 2016 8:12 am 
Offline
Member
Member

Joined: Fri Nov 16, 2012 4:16 am
Posts: 29
Location: Iran
I wrote a code with interrupt 1ah, and I did this :

Code:
mov al, ch
and al, 0fh
mov dl, al


Now, for example time is "18:36", it shall print hours, and only prints 8. Because I wanted program to do this. But, what can I do to show "1"?

P.S : I tested masking lower nibble, but it wasn't my answer.


Top
 Profile  
 
 Post subject: Re: Printing a BCD value
PostPosted: Wed May 04, 2016 9:46 am 
Offline
Member
Member

Joined: Fri Jan 30, 2015 4:57 pm
Posts: 215
Location: Germany
Now shift the high nibble to the low one using shr al, 4.


Top
 Profile  
 
 Post subject: Re: Printing a BCD value
PostPosted: Wed May 04, 2016 10:02 am 
Offline
Member
Member

Joined: Fri Nov 16, 2012 4:16 am
Posts: 29
Location: Iran
Techel wrote:
Now shift the high nibble to the low one using shr al, 4.


It says "byte register cannot be first operand"


Top
 Profile  
 
 Post subject: Re: Printing a BCD value
PostPosted: Wed May 04, 2016 10:55 am 
Offline
Member
Member

Joined: Fri Jan 30, 2015 4:57 pm
Posts: 215
Location: Germany
Ok, didn't know shr/l don't accept byte registers. Then use the entire ax.


Top
 Profile  
 
 Post subject: Re: Printing a BCD value
PostPosted: Wed May 04, 2016 11:00 am 
Offline
Member
Member

Joined: Fri Nov 16, 2012 4:16 am
Posts: 29
Location: Iran
Techel wrote:
Ok, didn't know shr/l don't accept byte registers. Then use the entire ax.


Sorry, but even word register wasn't accepted :lol:


Top
 Profile  
 
 Post subject: Re: Printing a BCD value
PostPosted: Wed May 04, 2016 11:33 am 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5069
It sounds like there's something wrong with your assembler. It should accept shr al, 4 as a valid instruction in most situations. (It's not a valid 8086 instruction, so make sure your assembler is using 80186 instructions at minimum.)


Top
 Profile  
 
 Post subject: Re: Printing a BCD value
PostPosted: Wed May 04, 2016 11:35 am 
Offline
Member
Member

Joined: Fri Nov 16, 2012 4:16 am
Posts: 29
Location: Iran
Octocontrabass wrote:
It sounds like there's something wrong with your assembler. It should accept shr al, 4 as a valid instruction in most situations. (It's not a valid 8086 instruction, so make sure your assembler is using 80186 instructions at minimum.)



I use masm16.


Top
 Profile  
 
 Post subject: Re: Printing a BCD value
PostPosted: Wed May 04, 2016 1:54 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5069
MASM only accepts 8086 instructions by default. To use 80186 instructions like "shr al, 4" in your code, you need to enable the 80186 instruction set using the .186 directive.


Top
 Profile  
 
 Post subject: Re: Printing a BCD value
PostPosted: Wed May 04, 2016 2:43 pm 
Offline
Member
Member

Joined: Fri Nov 16, 2012 4:16 am
Posts: 29
Location: Iran
Finally, I switched to nasm and my code is now this :

Code:
mov bh, ch
shr bh, 4
and bh, 0fh
add bh, 30h


mov ah, 0x0e
mov al, bh
int 0x10

mov bh, ch
and bh, 0fh
add bh, 30h

mov ah, 0x0e
mov al, bh
int 0x10

mov ah,0x0e
mov al, ':'
int 0x10

mov bh, cl
shr bh, 4
and bh, 0fh
add bh, 30h

mov ah, 0x0e
mov al, bh
int 0x10

mov bh, cl
and bh, 0fh
add bh, 30h

mov ah, 0x0e
mov al, bh
int 0x10

mov ah,0x0e
mov al, ':'
int 0x10

mov bh, dh
shr bh, 4
and bh, 0fh
add bh, 30h

mov ah, 0x0e
mov al, bh
int 0x10

mov bh, dh
and bh, 0fh
add bh, 30h

mov ah, 0x0e
mov al, bh
int 0x10


Top
 Profile  
 
 Post subject: Re: Printing a BCD value
PostPosted: Wed May 04, 2016 10:13 pm 
Offline
Member
Member
User avatar

Joined: Sat Jan 15, 2005 12:00 am
Posts: 8561
Location: At his keyboard!
Hi,

Haghiri75 wrote:
Finally, I switched to nasm and my code is now this :


Cool. Now use some routines to make it smaller and easier to maintain, like this:

Code:
;Print a single character
;
;Input
;    al = character to print
;
;Output
;    none

printChar:
    push ax
    mov ah, 0x0e
    int 0x10
    pop ax
    ret



Code:
;Print a BCD value
;
;Input
;    al = BCD value to print
;
;Output
;    none

printBCD:
    push ax
    mov ah,al      ;ah = BCD value
    shr al,4       ;al = high nibble
    and ah,0x0F    ;ah = low nibble
    or ax,0x3030   ;al = high nibble + '0', ah = low nibble + '0'
    call printChar ;Print character for high nibble
    mov al,ah      ;al = character for low nibble
    call printChar ;Print character for low nibble
    pop ax
    ret


Code:
    mov al,ch        ;al = hour as BCD
    call printBCD

    mov al, ':'
    call printChar

    mov al,cl        ;al = minute as BCD
    call printBCD

    mov al, ':'
    call printChar

    mov al,dh        ;al = second as BCD
    call printBCD

:)


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.


Top
 Profile  
 
 Post subject: Re: Printing a BCD value
PostPosted: Sun May 08, 2016 7:43 pm 
Offline

Joined: Sun May 08, 2016 6:58 pm
Posts: 1
Off topic, but the 8086 can perform shr al,4. The op-code should be 0xD2 E8 with the CL register set to 4. So that compiler is mad.

Also, If you implemented a function that could print out hex codes, for example in order to print registers etc.. , it would also be able to print out BCD ( but you'd have to check for A-F first)


Top
 Profile  
 
 Post subject: Re: Printing a BCD value
PostPosted: Sun May 08, 2016 11:34 pm 
Offline
Member
Member
User avatar

Joined: Sat Jan 15, 2005 12:00 am
Posts: 8561
Location: At his keyboard!
Hi,

samreeve wrote:
Off topic, but the 8086 can perform shr al,4. The op-code should be 0xD2 E8 with the CL register set to 4. So that compiler is mad.


8086 only supported "shl reg,1" and "shl reg,cl". The "shl reg,imm8" variation was added by 80186.


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.


Top
 Profile  
 
 Post subject: Re: Printing a BCD value
PostPosted: Mon May 09, 2016 7:27 am 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5069
samreeve wrote:
Off topic, but the 8086 can perform shr al,4. The op-code should be 0xD2 E8 with the CL register set to 4. So that compiler is mad.

How do you propose the assembler generate code to set CL to the correct value? Does it simply insert a MOV and hope the programmer knows that it will clobber CL? Does it try to save/restore CX on the stack, even though the stack may not be usable?

There are no good solutions to that problem, so the assembler generates an error instead. If the programmer wants to shift right by 4 on an 8086, the programmer will have to explicitly use "shr al, cl".


Top
 Profile  
 
 Post subject: Re: Printing a BCD value
PostPosted: Mon May 09, 2016 8:14 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4591
Location: Chichester, UK
Octocontrabass wrote:
If the programmer wants to shift right by 4 on an 8086, the programmer will have to explicitly use "shr al, cl".
I'm pretty sure the fastest solution is just to do 4 "shr al, 1" instructions. And it doesn't clobber any other registers.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 6 guests


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 post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group