Re: Pass the parameter N in register CX; return result in register AX.




"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.
thx
Awesomer is when you reduce it down to just *one* instruction:

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.



.



Relevant Pages

  • Re: instruction rescheduler?
    ... assembly language have never written code in another assembler. ... > -- register allocation and keeping track of which register is what, ... Register allocation, globally, is one of those things a machine usually ... Assembly programmers don't want to believe that a machine could ...
    (comp.lang.asm.x86)
  • Re: *Exe Programm aus Assembler heraus =?UTF-8?B?YXVzZsO8aHJlbiAv?= =?UTF-8?B?NGJo?=
    ... lege in den Umschlag den Brief; lege in die Sachtel den Umschlag; lege in den Safe die Schachtel; ... lege den Brief in den Umschlag; lege den Umschlag in die Schachtel; lege die Schachtel in den Safe; ... die restlichen Register haben. ... Man benutzt gerade einen Assembler, ...
    (de.comp.lang.assembler.x86)
  • Re: Assembler?
    ... 6502 assembler I used was about 20K, and the disassembler was less than ... compilers optimized to them, they are optimised to the compilers and ... Register shmegister. ... What's cache RAM but a shitload of fast registers..especially for the stack?.. ...
    (comp.os.linux.misc)
  • Re: Two Click disassembly/reassembly
    ... > Map the extra x86 registers to memory. ... > As long as you've got indirection through a register and the ability to ... compiler can't handle here. ... rather than a line by line assembler. ...
    (alt.lang.asm)
  • Re: Catagories of Assembly Languages
    ... > mov ax, segreg1 ... > ..or without register trash: ... as you are if the assembler decided to trash the registers behind your back ... The rest are unfortunately HLL programmers that use inlined ...
    (alt.lang.asm)