flat assembler
Message board for the users of flat assembler.

Index > Main > How to emulate the "sar" operator

Author
Thread Post new topic Reply to topic
MCD



Joined: 21 Aug 2004
Posts: 604
Location: Germany
MCD
if you need a sar operand, try this:

Code:
sar     equ /1 shl
    


rol/ror cannot be emulated as simple

_________________
MCD - the inevitable return of the Mad Computer Doggy

-||__/
.|+-~
.|| ||
Post 25 Nov 2004, 19:59
View user's profile Send private message Reply with quote
comrade



Joined: 16 Jun 2003
Posts: 1137
Location: Russian Federation
comrade
i don't understand how that works?
Post 25 Nov 2004, 20:12
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17280
Location: In your JS exploiting you and your system
revolution
so:

x = var sar 4

assembles to:

x = var /1 shl 4

which means:

x = var/16

Nice Smile
Post 26 Nov 2004, 06:46
View user's profile Send private message Visit poster's website Reply with quote
comrade



Joined: 16 Jun 2003
Posts: 1137
Location: Russian Federation
comrade
ah good
that keeps sign bit?
Post 26 Nov 2004, 16:37
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
MCD



Joined: 21 Aug 2004
Posts: 604
Location: Germany
MCD
yes, that keeps sign bit, but is a bit slow.

But if anyone needs a really fast "sar" operator, then you should try implementing "sar" directly into Fasm. I had done this some time ago, but removed it because I didn't use it much! I also implemented "bsf" and "bsr" this way. "bsf" and "bsr" are unary operators, while "sar" is a binary one. If anyone needs such things, feel free to use it, but I don't thing Privalov should implement it in official releases (not used much enough)

Code:
;This file describes how to implement an "bsf", "bsr" and "sar" operator into Fasm.

;Changes to perform in "EXPRESSI.INC" follow.
;Search for the indicated commands.


;sar - operator:
;---------------

;After "cmp     al,0C1h
;       je      calculate_shr"
  cmp     al,0C3h
     je      calculate_sar
;After "zero_value:"
      calculate_sar:
     mov     edx,dword [edi+4]
   bt      edx,31
      jc      shr_negative
        mov     eax,[ebx+4]
 or      edx,edx
     jnz     sign_value
  mov     ecx,[edi]
   cmp     ecx,64
      jae     sign_value
  cmp     cl,32
       jae     sar_high
    mov     edx,[ebx]
   shrd    edx,eax,cl
  sar     eax,cl
      mov     [ebx],edx
   mov     [ebx+4],eax
 jmp     calculation_loop
      sar_high:
     cdq
 sub     cl,32
       sar     eax,cl
      mov     [ebx],eax
   mov     [ebx+4],edx
 jmp     calculation_loop
      sign_value:
   cdq
 mov     [ebx],edx
   mov     [ebx+4],edx
 jmp     calculation_loop


;bsf - operator:
;---------------

;After "cmp     al,0D1h
;       je      calculate_neg"
     cmp     al,0D2h
     je      calculate_bsf
       cmp     al,0D3h
     je      calculate_bsr
;After "calculate_neg:"
      calculate_bsf:
  xor     edx,edx
     cmp     [edi+8],dx
  jne     invalid_expression
  cmp     [edi+12],dl
 je      bsf_ok
      cmp     [error_line],edx
    jne     bsf_ok
      mov     eax,[current_line]
  mov     [error_line],eax
    mov     [error],invalid_use_of_symbol
      bsf_ok:
  bsf     eax,[edi]
   jnz     store_result_simple
 bsf     eax,[edi+4]
 add     al,32
      store_result_simple:
     mov     [edi],eax
   mov     [edi+4],edx
 add     edi,14h
     jmp     calculation_loop
      calculate_bsr:
        xor     edx,edx
     cmp     [edi+8],dx
  jne     invalid_expression
  cmp     [edi+12],dl
 je      bsr_ok
      cmp     [error_line],edx
    jne     bsr_ok
      mov     eax,[current_line]
  mov     [error_line],eax
    mov     [error],invalid_use_of_symbol
      bsr_ok:
  bsr     eax,[edi+4]
 lea     eax,[eax+32]
        jnz     store_result_simple
 bsr     eax,[edi]
   jmp     store_result_simple


;Changes to perform in "PARSER.INC" follow.
;Somewhere at the end of the file.

;Old:
operators:
 db 1,'+',80h
 db 1,'-',81h
 db 1,'*',90h
 db 1,'/',91h
 db 3,'mod',0A0h
 db 3,'and',0B0h
 db 2,'or',0B1h
 db 3,'xor',0B2h
 db 3,'shl',0C0h
 db 3,'shr',0C1h
 db 0

single_operand_operators:
 db 1,'+',0
 db 1,'-',0D1h
 db 3,'not',0D0h
 db 3,'rva',0E0h
 db 0

;New:
operators:
 db 1,'+',80h
 db 1,'-',81h
 db 1,'*',90h
 db 1,'/',91h
 db 3,'mod',0A0h
 db 3,'and',0B0h
 db 2,'or',0B1h
 db 3,'xor',0B2h
 db 3,'shl',0C0h
 db 3,'shr',0C1h
 db 3,'sar',0C3h
 db 0

single_operand_operators:
 db 1,'+',0
 db 1,'-',0D1h
 db 3,'not',0D0h
 db 3,'bsf',0D2h
 db 3,'bsr',0D3h
 db 3,'rva',0E0h
 db 0
    
Post 29 Nov 2004, 17:02
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.