Re: newbie questions text code...
- From: Jack Klein <jackklein@xxxxxxxxxxx>
- Date: Sat, 26 Aug 2006 20:14:56 -0500
On 26 Aug 2006 12:59:28 -0700, "waltbrad" <spamtrap@xxxxxxxxxx> wrote
in comp.lang.asm.x86:
Hello. I'm reading Jeff Duntemann's book "Assembly Language Step by
Step". So, I'm a total novice. I'm in Chapter 10 and questioning some
code he has. Now the code is totally academic and is used mostly to
illustrate the use of the AND, XOR and SHR instructions.
The author says DOS will not display numeric values from a register but
only text. So, to display those numeric values you first have to
convert them to their string representations through the use of a
table; hence this code.
The table is:
Digits DB "0123456789ABCDEF"
And the code in the book is thus:
Byte2Str:
mov DI, AX ; Duplicate byte in DI
At this time, DI contains whatever AX did, could be any hex value from
0000 to FFFF, if the function is called incorrectly.
and DI, 000FH ; Mask out high 12 bits of DI
Now DI contains the low nibble (four bits) that were originally in AX.
So there is no possibility of using a value greater than 15 to index
into the table.
mov BX, Digits ; Load offset of Digits into BX
mov AH, BYTE [BX+DI] ; Load digit from table into AH
mov [SI +1], AH ; and store digit into string
xor AH, AH ; Zero out AH
This instruction sets AH to, AL still contains the original 8 bits it
had at the beginning of the function. So AL = AH = 00 to FF hex.
mov DI, AX ; and move byte into DI
Now DI contains a value in the range 00 to FF hex.
shr DI, 4 ; Shift high nybble of byte to
Right shifting by does three things:
1. Shifts the low nibble off the right end into the bit bucket.
2. Shifts the high 12 bits of DI (15...4) to the low 12 bits
(11...0).
3. Shifts 0 bits into the high 4 bits of DI. This is the logical
right shift instruction, that always shifts 0 bits in from the left as
it goes. The other version of the right shift is the arithmetic right
shift SAL, which would shift in 0 bits from the left if bit 15 was 0,
but shift in 1 bits from the left if bit 15 was 1.
low
mov AH, BYTE [BX+DI] ;Load digit from table into AH
mov [SI], AH ; and store digit into string
ret ; We're done - go home
So at this point you're supposed to have the numeric value stored as a
string representation so you return back to you main program to have it
printed out.
My question is shouldn't he have included his AND instruction a second
time? Right after the SHR instruction? I know he has pushed the
lower four bits out of the question, and the upper four bits of the
first byte are now the lower four bits. But the upper eight bits of DI
are still contained in the middle 8 bits of the word.
It's true that the upper 8 bits of DI are still in the middle 8 bits
of DI after the shift, but because of the "xor ah, ah" before the
second "mov di, ax", the upper 8 bits of DI were all 0 before the
shift.
Simple question, I guess, but I am wondering if I'm missing something.
I think you were missing the effect of the xor ah, ah on the upper 8
bits of ax, which then become the upper 8 bits of di.
Thanks.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
.
- Follow-Ups:
- Re: newbie questions text code...
- From: waltbrad
- Re: newbie questions text code...
- References:
- newbie questions text code...
- From: waltbrad
- newbie questions text code...
- Prev by Date: Re: I feel stupid... "Invalid combination of opcode and operand", was, now is FORTH question
- Next by Date: Re: Could not open hla.hw
- Previous by thread: newbie questions text code...
- Next by thread: Re: newbie questions text code...
- Index(es):
Relevant Pages
|