LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   assembly language! please help me! thanks in advance! (https://www.linuxquestions.org/questions/programming-9/assembly-language-please-help-me-thanks-in-advance-874120/)

topheraholic 04-10-2011 09:47 AM

assembly language! please help me! thanks in advance!
 
how does AAA(ASCII adjust after addition) really works?
how does Binary-Coded Decimal addition really works?

how to change 006Ah to 0100h after use aaa?

Code:

mov ah, 0 
mov al,'8'    /* AX = 0038h(0011 1000)
add al,'2'    /* AX = 0038h + 0032h = 006Ah,right?
aaa          /* AX = 0100h, so why??


paulsm4 04-10-2011 12:46 PM

The key thing about "Binary Coded Decimal" (BCD) is that each byte only holds decimal digits (values between 0..9). It's often used in controller applications (e.g. a clock or timer that needs to display decimal values).

So the number "123" (decimal "one hundred twenty three") is represented like this:

Code:

Binary  BCD
------  ---
0x007b  0x0123

The example above used "packed BCD" - two digits per byte. As you can see, its less efficient than binary.

"Unpacked BCD" is even LESS efficient: you only store ONE decimal digit per byte.

Here are two good links:
http://en.wikipedia.org/wiki/Intel_BCD_opcode
http://www.arl.wustl.edu/~lockwood/c..._6/CH06-2.html
<= Search for "aaa and daa instructions"

PS:
As you're probably aware, the general BCD instructions (the kind you're using above) only support signed arithmetic.

PPS:
A good book (if I haven't already recommended it to you), is:
"Professional Assembly Language", Richard Blum

salasi 04-10-2011 02:39 PM

Quote:

Originally Posted by topheraholic (Post 4319995)
how does Binary-Coded Decimal addition really works?

Oh, stuff, I don't think that you really mean you want to know how BCD arith really works, but, just in case I'm wrong. If you go to http://www.educypedia.be/electronics...arithmetic.htm and download the datasheet on, eg, the 74F283, you'll see that on page 3 there is a logic diagram for a binary half adder. This is how a normal half-adder works.

Now further down that page, if you look at http://mayaweb.upr.clu.edu/~borges/Chp5.pdf, you'll see an excellent tutorial on how the more complex circuits, including BCD are constructed. And now you know that, you should be able to work out why the original Z80 has a four bit alu, why it didn't look like that to a programmer (ie, took two passes through the 4 bit alu to do eight bith math) and why it was a bad idea.

topheraholic 04-11-2011 08:01 AM

Quote:

Originally Posted by paulsm4 (Post 4320150)
The key thing about "Binary Coded Decimal" (BCD) is that each byte only holds decimal digits (values between 0..9). It's often used in controller applications (e.g. a clock or timer that needs to display decimal values).

So the number "123" (decimal "one hundred twenty three") is represented like this:

Code:

Binary  BCD
------  ---
0x007b  0x0123

The example above used "packed BCD" - two digits per byte. As you can see, its less efficient than binary.

"Unpacked BCD" is even LESS efficient: you only store ONE decimal digit per byte.

Here are two good links:
http://en.wikipedia.org/wiki/Intel_BCD_opcode
http://www.arl.wustl.edu/~lockwood/c..._6/CH06-2.html
<= Search for "aaa and daa instructions"

PS:
As you're probably aware, the general BCD instructions (the kind you're using above) only support signed arithmetic.

PPS:
A good book (if I haven't already recommended it to you), is:
"Professional Assembly Language", Richard Blum

thanks! but i donot understand this!! sorry!

topheraholic 04-11-2011 08:03 AM

Quote:

Originally Posted by salasi (Post 4320233)
Oh, stuff, I don't think that you really mean you want to know how BCD arith really works, but, just in case I'm wrong. If you go to http://www.educypedia.be/electronics...arithmetic.htm and download the datasheet on, eg, the 74F283, you'll see that on page 3 there is a logic diagram for a binary half adder. This is how a normal half-adder works.

Now further down that page, if you look at http://mayaweb.upr.clu.edu/~borges/Chp5.pdf, you'll see an excellent tutorial on how the more complex circuits, including BCD are constructed. And now you know that, you should be able to work out why the original Z80 has a four bit alu, why it didn't look like that to a programmer (ie, took two passes through the 4 bit alu to do eight bith math) and why it was a bad idea.

i just wanna know how this works?do you know?thanks!
Code:

mov ah, 0 
mov al,'8'    /* AX = 0038h(0011 1000)
add al,'2'    /* AX = 0038h + 0032h = 006Ah,right?
aaa          /* AX = 0100h, so why??

why AX is 0100h?


All times are GMT -5. The time now is 03:25 PM.