Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Assembly x86 > Re: Any more gr...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 39 of 39 Topic 4612 of 4729
Post > Topic >>

Re: Any more graceful way to saturate adds?

by pete <spamtrap@[EMAIL PROTECTED] > May 2, 2008 at 04:38 AM

In article 
<25740676-bb54-4871-b103-d3b7135e4f28@[EMAIL PROTECTED]
>
           spamtrap@[EMAIL PROTECTED]
 "Jim Leonard" writes:

> On Apr 22, 1:01 pm, Terje Mathisen  <spamt...@[EMAIL PROTECTED]
> wrote:
> > BTW, there is a very sneaky way to avoid that initial jump:
> >
> > Instead of
> >
> >   jmp short start
> >   next:
> >     stosb
> > start:
> >
> > you can instead write:
> >
> >    mov al,0AAh
> > org $-1         ; Back up one byte into the immediate constant above
> > next:
> >    stosb
> >
> > I.e. on the first iteration we do a dummy two-byte instruction with a
> > one-byte immediate, which we then make sure contains the STOSB opcode
byte.
> >
> > The gain is that we save one opcode byte (MOV instead of JMP SHORT)
and
> > a taken branch, so the actual cost of rotating the loop like this is 8
> > cycles on the first iteration, instead of 17, and each iteration saves
> > two opcode bytes or 8 cycles.
> 
> I hate to seem more daft than I already am, but after re-reading the
> above for a week I still don't get the trick.

The "trick" is to locate the next: label in the middle of the 
instruction 'mov al,imm8' -- the 'org $-1' winds back the program 
counter one location and assembles the STOSB opcode in the 
location occupied by the imm8 value of the previous instruction.  
This results in a benign 'mov al,0AAh' first time through the 
code (benign in the sense that AL will soon after be loaded with 
the proper value) but when looping back to the next: label the 
code will find a STOSB (i.e. jumps into the middle of the mov 
instruction).

>  I thought ORG was for
> specifying where the assembler assumes the binary code will get
> loaded; how does that save two opcode bytes per iteration?

This saving comes from having a single STOSB at the "top" of the 
loop; the alternative earlier loop had 3 STOSB instructions (one 
in each branch) -- hence the saving of 2 x STOSB.

Very neat and goes some way to justify my earlier comment about 
Terje's little finger ;-)

Pete

Incidentally, I've used the 'org $-1' technique in the past for 
self-modifying code to jump to alternative addresses (like a 
switch() in C).  Something like

        jmp short +2
        org $-1
disp    db ?

proc1:  some code
        ret
proc2:  more code etc...
        ret

Then, at some point before the jmp you just load up the 
displacement byte (disp) with 0, (proc2-proc1), (proc3-proc1), 
etc. according to which section of code you want to jump to.  
Not much use on modern processors I guess, but it worked well on 
the 8086 and z80!
-- 
   "We have not inherited the earth from our ancestors,
    we have borrowed it from our descendants."
 




 39 Posts in Topic:
Any more graceful way to saturate adds?
Jim Leonard <spamtrap  2008-04-10 14:59:55 
Re: Any more graceful way to saturate adds?
(Dick Wesseling) <spam  2008-04-11 02:32:55 
Re: Any more graceful way to saturate adds?
Terje Mathisen <spamt  2008-04-11 09:41:42 
Re: Any more graceful way to saturate adds?
Jim Leonard <spamtrap  2008-04-11 11:05:19 
Re: Any more graceful way to saturate adds?
Terence <spamtrap@[EM  2008-04-11 16:40:10 
Re: Any more graceful way to saturate adds?
Terence <spamtrap@[EM  2008-04-11 23:28:22 
Re: Any more graceful way to saturate adds?
"Wolfgang Kern"  2008-04-12 12:51:54 
Re: Any more graceful way to saturate adds?
Robert Redelmeier <red  2008-04-12 19:29:18 
Re: Any more graceful way to saturate adds?
Richard Russell <spam  2008-04-12 13:52:03 
Re: Any more graceful way to saturate adds?
Terje Mathisen <spamt  2008-04-13 15:31:59 
Re: Any more graceful way to saturate adds?
Terence <spamtrap@[EM  2008-04-12 17:03:07 
Re: Any more graceful way to saturate adds?
"Rod Pemberton"  2008-04-12 23:06:56 
Re: Any more graceful way to saturate adds?
"Rod Pemberton"  2008-04-12 23:10:57 
Re: Any more graceful way to saturate adds?
Terje Mathisen <spamt  2008-04-13 15:00:06 
Re: Any more graceful way to saturate adds?
"Rod Pemberton"  2008-04-13 11:27:29 
Re: Any more graceful way to saturate adds?
"Alexei A. Frounze&q  2008-04-13 17:19:25 
Re: Any more graceful way to saturate adds?
"Rod Pemberton"  2008-04-14 02:41:38 
Re: Any more graceful way to saturate adds?
Jim Leonard <spamtrap  2008-04-14 06:58:12 
Re: Any more graceful way to saturate adds?
"Alexei A. Frounze&q  2008-04-14 03:39:12 
Re: Any more graceful way to saturate adds?
pete <spamtrap@[EMAIL   2008-04-15 05:11:19 
Re: Any more graceful way to saturate adds?
Jim Leonard <spamtrap  2008-04-15 07:30:34 
Re: Any more graceful way to saturate adds?
pete <spamtrap@[EMAIL   2008-04-17 17:05:43 
Re: Any more graceful way to saturate adds?
Terje Mathisen <spamt  2008-04-17 13:14:08 
Re: Any more graceful way to saturate adds?
Danjel McGougan <spam  2008-04-18 02:31:30 
Re: Any more graceful way to saturate adds?
Terje Mathisen <spamt  2008-04-18 11:21:01 
Re: Any more graceful way to saturate adds?
dave <spamtrap@[EMAIL   2008-04-18 13:57:11 
Re: Any more graceful way to saturate adds?
Jim Leonard <spamtrap  2008-04-21 07:23:39 
Re: Any more graceful way to saturate adds?
Jim Leonard <spamtrap  2008-04-21 07:33:56 
Re: Any more graceful way to saturate adds?
Esra Sdrawkcab <spamt  2008-04-21 20:07:15 
Re: Any more graceful way to saturate adds?
Terje Mathisen <spamt  2008-04-22 11:01:06 
Re: Any more graceful way to saturate adds?
Esra Sdrawkcab <spamt  2008-04-23 08:20:41 
Re: Any more graceful way to saturate adds?
"H. Peter Anvin"  2008-06-02 09:43:53 
Re: Any more graceful way to saturate adds?
Terje Mathisen <spamt  2008-06-02 19:53:52 
Re: Any more graceful way to saturate adds?
aku ankka <spamtrap@[  2008-04-22 00:38:05 
Re: Any more graceful way to saturate adds?
Terje Mathisen <spamt  2008-04-22 11:03:00 
Re: Any more graceful way to saturate adds?
aku ankka <spamtrap@[  2008-04-22 23:43:47 
Re: Any more graceful way to saturate adds?
Jim Leonard <spamtrap  2008-05-01 13:13:06 
Re: Any more graceful way to saturate adds?
Terje Mathisen <spamt  2008-05-02 09:11:22 
Re: Any more graceful way to saturate adds?
pete <spamtrap@[EMAIL   2008-05-02 04:38:05 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu Jul 24 14:59:57 CDT 2008.