Re: Pass the parameter N in register CX; return result in register AX.
- From: "cr88192" <cr88192@xxxxxxxxxxxxxxxxxx>
- Date: Sat, 28 Apr 2007 06:36:57 +1000
"Evenbit" <nbaker2328@xxxxxxxxxxx> wrote in message
news:1177703794.128478.288680@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
On Apr 27, 3:14 pm, Herbert Kleebauer <k...@xxxxxxxxx> wrote:
Evenbit wrote:
i know there should be an easy way to do that. i need your help
S = 1 + 2 + 4 + 8 + 16 + 32 + .......+ 2N
Pass the parameter N in register CX; return result in register
AX.
how can i do that?
mov ax,cx ;ie cx=16 (= max. for 16-bit registers)
dec ax ;-1 due bit numbers start with 0
bts ax,ax ;set bit 15 produces 0x8000
dec ax ;reverse all lower bits 0x7fff
add ax,cx ;add MSbit =0xffff =65535
65535 =
32768+16384+8192+4096+2048+1024+512+256+128+64+32+16+8+4+2+1
mov ax,cx ;ie cx=3
dec ax ;-1 due bit numbers start with 0
bts ax,ax ;set bit 2 produces 0x0004
dec ax ;reverse all lower bits 0x0003
add ax,cx ;add MSbit =0x0007
7 = 4+2+1
homework done yet :)
that was awesome.Awesomer is when you reduce it down to just *one* instruction:
thx
mov ax, [table+cx]
In opposite to Wolfgangs answer this is even obvious
for a beginner that this can't work!
prefix it with 'shl cx, 1' and it does...
no, I think the point is that this is 16-bit code, and thus a memref such
as:
[table+cx] or [table+cx*2]
is non-encodable (this requires 32-bit instruction encoding).
reason:
16 bit opcodes use a different mod/rm byte structure than 32 bit code.
so, you coud write:
[table+bx]
[table+bp]
[table+si]
[table+di]
[table+bx+si]
[table+bx+di]
[table+bp+si]
[table+bp+di]
but not:
[table+cx]
instead, this feature is given via the 32-bit SIB byte.
actually, I was the opposite:
I learned assembler coding back in dos days;
I had moved to 32 bits, and somehow never realized the full power of the SIB
byte (ie: noting that I could use pretty much any reg I wanted as a pointer
or index).
after writing my own assembler, I came to understand this much better (and
thus wrote much better assembly code as a result...).
now, long-mode is different still, it adds a few more possibilities.
Nathan.
.
- References:
- Pass the parameter N in register CX; return result in register AX.
- From: aeter
- Re: Pass the parameter N in register CX; return result in register AX.
- From: Wolfgang Kern
- Re: Pass the parameter N in register CX; return result in register AX.
- From: aeter
- Re: Pass the parameter N in register CX; return result in register AX.
- From: Evenbit
- Re: Pass the parameter N in register CX; return result in register AX.
- From: Herbert Kleebauer
- Re: Pass the parameter N in register CX; return result in register AX.
- From: Evenbit
- Pass the parameter N in register CX; return result in register AX.
- Prev by Date: Re: Where, oh where, is Frank
- Next by Date: Re: Somewhat offtopic: any suggestions/wishes for a programming language amenable to strong optimisation?
- Previous by thread: Re: Pass the parameter N in register CX; return result in register AX.
- Next by thread: Re: Pass the parameter N in register CX; return result in register AX.
- Index(es):
Relevant Pages
|