Use backtick code blocks for syntax highlighting
This commit is contained in:
parent
8ed9dea394
commit
f658bfbb40
203 changed files with 26546 additions and 25801 deletions
76
09-01.md
76
09-01.md
|
|
@ -88,13 +88,17 @@ machine word (32 bits in 386 protected mode, 16 bits otherwise), but it
|
|||
renders **LEA** useless for multiword operations, which use the Carry
|
||||
flag to tie together partial results. For example, these instructions
|
||||
|
||||
ADD EAX,EBX
|
||||
ADC EDX,ECX
|
||||
```nasm
|
||||
ADD EAX,EBX
|
||||
ADC EDX,ECX
|
||||
```
|
||||
|
||||
could *not* be replaced
|
||||
|
||||
LEA EAX,[EAX+EBX]
|
||||
ADC EDX,ECX
|
||||
```nasm
|
||||
LEA EAX,[EAX+EBX]
|
||||
ADC EDX,ECX
|
||||
```
|
||||
|
||||
because **LEA** doesn't affect the Carry flag.
|
||||
|
||||
|
|
@ -103,16 +107,18 @@ performing pointer arithmetic, however. For instance, the following code
|
|||
uses **LEA** to advance the pointers while adding one 128-bit memory
|
||||
variable to another such variable:
|
||||
|
||||
MOV ECX,4 ;# of 32-bit words to add
|
||||
CLC
|
||||
;no carry into the initial ADC
|
||||
ADDLOOP:
|
||||
```nasm
|
||||
MOV ECX,4 ;# of 32-bit words to add
|
||||
CLC
|
||||
;no carry into the initial ADC
|
||||
ADDLOOP:
|
||||
|
||||
MOV EAX,[ESI] ;get the next element of one array
|
||||
ADC [EDI],EAX ;add it to the other array, with carry
|
||||
LEA ESI,[ESI+4] ;advance one array's pointer
|
||||
LEA EDI,[EDI+4] ;advance the other array's pointer
|
||||
LOOP ADDLOOP
|
||||
MOV EAX,[ESI] ;get the next element of one array
|
||||
ADC [EDI],EAX ;add it to the other array, with carry
|
||||
LEA ESI,[ESI+4] ;advance one array's pointer
|
||||
LEA EDI,[EDI+4] ;advance the other array's pointer
|
||||
LOOP ADDLOOP
|
||||
```
|
||||
|
||||
(Yes, I could use **LODSD** instead of **MOV/LEA**; I'm just
|
||||
illustrating a point here. Besides, **LODS** is only 1 cycle faster than
|
||||
|
|
@ -140,9 +146,11 @@ of my favorites:
|
|||
|
||||
John's code for setting AX to its absolute value is:
|
||||
|
||||
CWD
|
||||
XOR AX,DX
|
||||
SUB AX,DX
|
||||
```nasm
|
||||
CWD
|
||||
XOR AX,DX
|
||||
SUB AX,DX
|
||||
```
|
||||
|
||||
This does nothing when bit 15 of AX is 0 (that is, if AX is positive).
|
||||
When AX is negative, the code "nots" it and adds 1, which is exactly how
|
||||
|
|
@ -150,10 +158,12 @@ you perform a two's complement negate. For the case where AX is not
|
|||
negative, this trick usually beats the stuffing out of the standard
|
||||
absolute value code:
|
||||
|
||||
AND AX,AX ;negative?
|
||||
JNS IsPositive ;no
|
||||
NEG AX ;yes,negate it
|
||||
IsPositive:
|
||||
```nasm
|
||||
AND AX,AX ;negative?
|
||||
JNS IsPositive ;no
|
||||
NEG AX ;yes,negate it
|
||||
IsPositive:
|
||||
```
|
||||
|
||||
However, John's code is slower on a 486; as you're no doubt coming to
|
||||
realize (and as I'll explain in Chapters 12 and 13), the 486 is an
|
||||
|
|
@ -162,19 +172,23 @@ optimization world unto itself.
|
|||
Here's how John copies a block of bytes from DS:SI to ES:DI, moving as
|
||||
much data as possible a word at a time:
|
||||
|
||||
SHR CX,1 ;word count
|
||||
REP MOVSW ;copy as many words as possible
|
||||
ADC CX,CX ;CX=1 if copy length was odd,
|
||||
;0 else
|
||||
REP MOVSB ;copy any odd byte
|
||||
```nasm
|
||||
SHR CX,1 ;word count
|
||||
REP MOVSW ;copy as many words as possible
|
||||
ADC CX,CX ;CX=1 if copy length was odd,
|
||||
;0 else
|
||||
REP MOVSB ;copy any odd byte
|
||||
```
|
||||
|
||||
(**ADC CX,CX** can be replaced with **RCL CX,1**; which is faster
|
||||
depends on the processor type.) It might be hard to believe that the
|
||||
above is faster than this:
|
||||
|
||||
SHR CX,1 ;word count
|
||||
REP MOVSW ;copy as many words as
|
||||
;possible
|
||||
JNC CopyDone ;done if even copy length
|
||||
MOVSB ;copy the odd byte
|
||||
CopyDone:
|
||||
```nasm
|
||||
SHR CX,1 ;word count
|
||||
REP MOVSW ;copy as many words as
|
||||
;possible
|
||||
JNC CopyDone ;done if even copy length
|
||||
MOVSB ;copy the odd byte
|
||||
CopyDone:
|
||||
```
|
||||
|
|
|
|||
Loading…
Reference in a new issue