how to best optimization this code?



Code:
    mov ecx,1024*768
cycle:
      xor ebx,ebx
      mov bx,word ptr  fs:
      shl ebx,1
      mov ax,fs:
      mov fs:,ax
      add edi,2
      dec ecx
      cmp ecx,0
      jnz cycle


Posted on 2007-11-13 15:43:20 by korte

  push ds
  push fs
  pop ds

  xor eax,eax
  xor ebx,ebx
  mov ecx,1024*768
  mov edx,2
  mov ebp,1
cycle:
  mov bx,
  mov ax,
  mov ,ax
  add edi,edx
  sub ecx,ebp
  jnz cycle

  pop ds
Posted on 2007-11-13 15:44:35 by korte
As a general rule, when iterating over a block of memory where you have both a counter and an incrementing index, you can use the index itself as the counter saving at least 1 instruction and 1 register...

this would be the inner loop I would use:


imagesize EQU (1024 * 768 * 2)

mov ebp, -imagesize  ; the negative of the buffers size in bytes
sub edi, ebp             ; edi points to the first word AFTER the buffer
@@do:
movzx eax, word ptr ; grab pixel
movzx eax, word ptr ; convert pixel
mov , ax ; write pixel
add ebp, 2 ; iterate until...
js @@do ; ebp = 0


ebp here doubles as both the counter and the pointer into the buffer
Posted on 2007-11-13 18:50:25 by Rockoon