flat assembler
Message board for the users of flat assembler.

Index > Main > Conditional moving without branching

Goto page 1, 2  Next
Author
Thread Post new topic Reply to topic
AlexP



Joined: 14 Nov 2007
Posts: 561
Location: Out the window. Yes, that one.
AlexP 03 Apr 2008, 01:24
I vaguely remember how to do it with two choices, and I remember reading it SOMEWHERE, but how would you pull off a conditional move between three different choices? ( without branching ).

What I have is: 128,192,256. What I need is: 160,192,224 respectively. I have been trying but cannot get it working. I do not want the user to input those three values instead of the norm, nor do I want to reverse the process that these values are for. How do I do this??
Post 03 Apr 2008, 01:24
View user's profile Send private message Visit poster's website Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4347
Location: Now
edfed 03 Apr 2008, 01:34
can you repeat the question please, do not understand...
you can try with a lookup table followed by an addition to obtain numbers more than 255.
or cmovcc where cc is the condition code. but is ok for P II +.
Post 03 Apr 2008, 01:34
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4046
Location: vpcmpistri
bitRAKE 03 Apr 2008, 02:02
does this work?
Code:
cmp eax,192
sbb ebx,ebx
cmp eax,256
sbb ecx,ecx
and ebx,32
and ecx,12
add eax,ebx
lea eax,[eax+ecx-12]    

_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup
Post 03 Apr 2008, 02:02
View user's profile Send private message Visit poster's website Reply with quote
AlexP



Joined: 14 Nov 2007
Posts: 561
Location: Out the window. Yes, that one.
AlexP 03 Apr 2008, 02:15
Sorry BitRAKE, your code:
Code:
    xor  ebx, ebx
    xor  ecx, ecx
    mov  eax, 128
    cmp eax,192
    sbb ebx,ebx
    cmp eax,256
    sbb ecx,ecx
    and ebx,32
    and ecx,12
    add eax,ebx
    lea eax,[eax+ecx-12]
    

produces 244 for the input of 256, not what I needed... It did work for the other two though. I was wondering if this code, the one I regret to use due to branch prediction, would be okay:
Code:
mov eax, 224 ; default
mov ebx, 160 ; other two
mov ecx, 190
cmp edi, 192
cmove eax, ecx
cmovb eax, ebx
    

If anyone else wants a go, please do!


Last edited by AlexP on 03 Apr 2008, 02:35; edited 1 time in total
Post 03 Apr 2008, 02:15
View user's profile Send private message Visit poster's website Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4347
Location: Now
edfed 03 Apr 2008, 02:34
are you sure you will have only 3 values?
as i see it is a brief list of normalised samples rates.
http://en.wikipedia.org/wiki/Bitrate
bitrate, not bitrake. Very Happy

in case of many values, a translation table is the best choice.
Post 03 Apr 2008, 02:34
View user's profile Send private message Visit poster's website Reply with quote
AlexP



Joined: 14 Nov 2007
Posts: 561
Location: Out the window. Yes, that one.
AlexP 03 Apr 2008, 02:37
edfed: Yes, I'm sure I only have three values. You must be mistaking the snippet for something different.
Post 03 Apr 2008, 02:37
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20355
Location: In your JS exploiting you and your system
revolution 03 Apr 2008, 02:46
I know what these values are for, SHA code. Nothing else would use values like that. But why not a look-up table:
Code:
table: dd 160,192,224
...
mov eax,[input_value]
shr eax,6 ;edited from 7, thanks edfed
mov eax,[table+eax*4-8]    


Last edited by revolution on 03 Apr 2008, 03:05; edited 1 time in total
Post 03 Apr 2008, 02:46
View user's profile Send private message Visit poster's website Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4347
Location: Now
edfed 03 Apr 2008, 02:59
your code is ambiguous. 192 shr 7 = 128 shr 7.
Code:
table dd 160,192,224
...
mov eax,[value]
shr eax,6
mov eax,[table+eax*4-8]
    


i was thinking it was the common values of MP3 bit rates.
Post 03 Apr 2008, 02:59
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20355
Location: In your JS exploiting you and your system
revolution 03 Apr 2008, 03:04
Yes, you are correct. I'll edit above just to avoid confusion.
Post 03 Apr 2008, 03:04
View user's profile Send private message Visit poster's website Reply with quote
sinsi



Joined: 10 Aug 2007
Posts: 790
Location: Adelaide
sinsi 03 Apr 2008, 03:29
Code:
 mov eax,[value]
 mov ecx,160
 mov edx,224
 cmp eax,192   ;if eax=192 then it doesn't change
 cmovb eax,ecx ;if eax=128 then eax=160
 cmova eax,edx ;if eax=256 then eax=224
    
Post 03 Apr 2008, 03:29
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4347
Location: Now
edfed 03 Apr 2008, 03:45
Quote:
I was wondering if this code, the one I regret to use due to branch prediction, would be okay:

Code:
 
mov eax, 224 ; default 
mov ebx, 160 ; other two 
mov ecx, 190 
cmp edi, 192 
cmove eax, ecx 
cmovb eax, ebx 
    

Post 03 Apr 2008, 03:45
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4046
Location: vpcmpistri
bitRAKE 03 Apr 2008, 03:59
AlexP wrote:
Sorry BitRAKE, your code produces 244 for the input of 256
This should work then:
Code:
cmp eax,192 
sbb ebx,ebx 
cmp eax,256 
sbb ecx,ecx 
and ebx,32 
and ecx,32 
add eax,ebx 
lea eax,[eax+ecx-32]    
I just type and post - leave the testing to you.

_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup
Post 03 Apr 2008, 03:59
View user's profile Send private message Visit poster's website Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1903
DOS386 03 Apr 2008, 07:03
> What I have is: 128,192,256.
> What I need is: 160,192,224 respectively.

Code:
; Input in AL, AX or EAX
sub al,128
shr al,1
add al,160
; Result in AL, AX or EAX
    


I haven't seen an easier ASM "puzzle"
and a more silly "justification" for "cool"
new "useful" instructions before Very Happy

_________________
Bug Nr.: 12345

Title: Hello World program compiles to 100 KB !!!

Status: Closed: NOT a Bug
Post 03 Apr 2008, 07:03
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4347
Location: Now
edfed 03 Apr 2008, 12:54
al cannot contain 9 bits.
256 ==> 9 bits.

(192-128)/2+160!=192
(256-128)/2+160!=224

your easy puzzle resolution is completelly false.
you presume of your power. Rolling Eyes
Code:
  1000_0000b ==> 1010_0000b     128 to 160

  1100_0000b ==> 1100_0000b     192 to 192

1_0000_0000b ==> 1110_0000b     256 to 224

    


this puzzle should be very easy to solve in PURE digital electronics.TTL or CMOS.
Post 03 Apr 2008, 12:54
View user's profile Send private message Visit poster's website Reply with quote
AlexP



Joined: 14 Nov 2007
Posts: 561
Location: Out the window. Yes, that one.
AlexP 03 Apr 2008, 13:28
Quote:
I know what these values are for, SHA code. Nothing else would use values like that. But why not a look-up table:

These values are actually to calculate the byte offset of the end of an AES decryption key schedule, I think I will reverse the schedule instead of having this odd code run every decryption time. Very intersesting solutions though, thanks! They did give me an idea of how to speed up another part of code, good luck with storing 256 into al DOS386 Smile.
Post 03 Apr 2008, 13:28
View user's profile Send private message Visit poster's website Reply with quote
Goplat



Joined: 15 Sep 2006
Posts: 181
Goplat 03 Apr 2008, 14:19
This is probably the fastest way to do it:
Code:
shr     eax,1
add    eax,96    
Post 03 Apr 2008, 14:19
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20355
Location: In your JS exploiting you and your system
revolution 03 Apr 2008, 14:26
Goplat wrote:
This is probably the fastest way to do it:
Code:
shr  eax,1
add    eax,96    
Very nice.
Post 03 Apr 2008, 14:26
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4046
Location: vpcmpistri
bitRAKE 03 Apr 2008, 15:56
Most likely the smallest too.

_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup
Post 03 Apr 2008, 15:56
View user's profile Send private message Visit poster's website Reply with quote
Remy Vincent



Joined: 16 Sep 2005
Posts: 155
Location: France
Remy Vincent 03 Apr 2008, 21:02
Code:
org 100h
use16
mov ax,128
mov bx,192
mov cx,256
mov dx,111
push dx  cx  bx  ax
push dx  cx  bx  ax
push ds
pop es
;---------------  Display all values  ---------------
mov di,C_Buf25
mov al," "
stosb
mov al," "
stosb
mov al," "
stosb
pop ax
call IntegerToEStrZ
mov al,"."
stosb          ; At the end of the string, set  .
pop ax
call IntegerToEStrZ
mov al,"."
stosb          ; At the end of the string, set  .
pop ax
call IntegerToEStrZ
mov al,"."
stosb          ; At the end of the string, set  .
pop ax
call IntegerToEStrZ
mov al,0Dh
stosb
mov al,0Ah
stosb
mov [di],byte "$"   ; At the end of the string, set  $
mov dx,C_Buf25
mov ah,09h     ; INT 21h , Function 09h , Display String...$
int 21h
;---------------  Check values  ---------------
   pop ax  bx  cx  dx
   cmp ax,128
   jne IsOk_AX
   mov ax,160
IsOk_AX:
   cmp bx,192
   jne IsOk_BX
   mov bx,192  ; The same !!!
IsOk_BX:
   cmp cx,256
   jne IsOk_CX
   mov cx,224
IsOk_CX:
   cmp dx,111
   jne IsOk_DX
;--mov dx,dx-- ; We don't know if value 4 is to be checked
IsOk_DX:
   push dx  cx  bx  ax
;---------------  Display all values  ---------------
mov di,C_Buf25
mov al,"="
stosb
mov al,"="
stosb
mov al,">"
stosb
pop ax
call IntegerToEStrZ
mov al,"."
stosb          ; At the end of the string, set  .
pop ax
call IntegerToEStrZ
mov al,"."
stosb          ; At the end of the string, set  .
pop ax
call IntegerToEStrZ
mov al,"."
stosb          ; At the end of the string, set  .
pop ax
call IntegerToEStrZ
mov [di],BYTE "$"   ; At the end of the string, set  $
mov dx,C_Buf25
mov AH,09h     ; INT 21h , Function 09h , Display String...$
int 21h
;---------------  /  ---------------
int 20h


;=============== Data


   C_Buf25 DB   "TEMP BUFFER, SIZE......25"


;=============== Small toolbox


;===== IntegerToEStrZ

IntegerToEStrZ:
;AX(I_Number) , ES:DI(I_BufPtr)    ==>    ES:DI(O_EndingZ)
cld
;----- Check sign
or ax,ax
jns IntegerToEStrZ_ISPOSITIVE
IntegerToEStrZ_ISNEGATIVE:
mov cl,2Dh     ; Store sign
mov [di],CL
inc di
neg ax         ; Keep Abs(I_Number)
sbb ax,0FFFFh
IntegerToEStrZ_ISPOSITIVE:
mov bl,0       ; BL (L_StrModifiedQ)
mov cx,10000   ; CX (L_Divisor)
;----- LOOP , Char 1..4  ( NNNNx )
IntegerToEStrZ_LOOP:
xor dx,dx
div cx         ; DX:AX div CX  ==>  DX(rest)   AX(result)
;----- Ignore left  0
or al,al
jnz IntegerToEStrZ_STORE
or bl,bl
jz IntegerToEStrZ_NEXT
IntegerToEStrZ_STORE:
add al,30h
stosb
mov bl,1       ; Modified
IntegerToEStrZ_NEXT:
mov ax,dx
;----- 10000 ==> 1000  ( ==> 100 ==> 10 )
cmp cx,10
je IntegerToEStrZ_LAST
xor dx,dx
xchg ax,cx
mov si,10
div si
xchg ax,cx
jmp IntegerToEStrZ_LOOP
IntegerToEStrZ_LAST:
;----- Store last char  ( xxxxN )
add al,30h
stosb
;----- Store ending char  0
mov al,0
mov [di],al
ret    


Description: 128 & 192 & 256 values ==> 160 & 192 & 224 values
Download
Filename: 128.192.256.zip
Filesize: 2.96 KB
Downloaded: 725 Time(s)

Post 03 Apr 2008, 21:02
View user's profile Send private message Visit poster's website Reply with quote
Goplat



Joined: 15 Sep 2006
Posts: 181
Goplat 03 Apr 2008, 22:19
bitRAKE wrote:
Most likely the smallest too.
Actually, it could be one byte smaller by changing "add eax,96" to "add al,96".
Post 03 Apr 2008, 22:19
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2  Next

< 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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.