flat assembler
Message board for the users of flat assembler.

Index > Main > bswap directive

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



Joined: 16 Jun 2003
Posts: 1137
Location: Russian Federation
comrade
Similar to rva style, maybe something that will bswap given parameter?
Code:
mov eax, dword bswap 1234ABCDh ; eax has 12 34 AB CD, or 0CDAB3412h
mov cx, word bswap 1234h ; cx has 12 34, or 3412h    

_________________
comrade (comrade64@live.com; http://comrade.ownz.com/)
Post 08 Apr 2004, 00:58
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
ShortCoder



Joined: 07 May 2004
Posts: 105
ShortCoder
Is this really necessary? I mean, couldn't you just write
Code:
mov eax, 0CDAB3412h
mov cx,3412h
    

in the first place?
Post 07 May 2004, 06:32
View user's profile Send private message Reply with quote
comrade



Joined: 16 Jun 2003
Posts: 1137
Location: Russian Federation
comrade
its for readability

_________________
comrade (comrade64@live.com; http://comrade.ownz.com/)
Post 07 May 2004, 14:42
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
comrade: just a note: it wouldn't be a directive, it would be (numeric) operator
Post 07 May 2004, 19:22
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
ShortCoder



Joined: 07 May 2004
Posts: 105
ShortCoder
vid wrote:
comrade: just a note: it wouldn't be a directive, it would be (numeric) operator


No, I think he means directive as in a preprocessing directive (instructions that are assembler/compiler specific which are processed before the main assembly/compilation pass starts)

IMO, it is just as readable (to me) without such a directive, but then again I can easily read things left to right, right to left, top to bottom, etc... but if enough people like the idea, it might be nice to be implemented (I'm just afraid it might confuse some people with the actual bswap assembly language instruction, though)

Depending on how fasm is programmed to parse the input, it might be a bit difficult since it is an assembly language instruction as well (were it to be implemented) I know the source is there and I'll probably read it sometime, but I haven't yet so I just don't know.
Post 08 May 2004, 08:37
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
ShortCoder: no, it wouldn't be directive if it would work this way. a you said, "instruction, that is ...", that means directive have to be in place for instruction, not in instruction arguments. Instruction is first symbol (word) at the beggining of line or behind ":" of label definition. If it would be preprocessor directive (anyway, it would be preprocessor operator, not directive), it could work only with numeric constants, not with expressions or labels.

Also i don't believe privalov will add something that can be acheived another way:
Code:
macro loadbswap name,value
{
virtual at 0
  db value shr 24 and 0FFh
  db value shr 16 and 0FFh
  db value shr 8 and 0FFh
  db value and 0FFh
  load name from 0
end virtual
}
    


Last edited by vid on 08 May 2004, 18:04; edited 1 time in total
Post 08 May 2004, 17:28
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Posetf



Joined: 01 Mar 2004
Posts: 35
Location: London
Posetf
Hi,
I was adding rol and ror, so I chucked bswap in at the same time.
Here's hoping this works for you. I only did word and dword, ok?

In Expressi.inc, search for calculate_neg.
After 'je calculate_neg', insert:

cmp al,0D2h
je calculate_bswap

Before 'calculate_neg:', insert:

calculate_bswap:
cmp word [edi+8],0
jne invalid_expression
cmp byte [edi+12],0
je bswap_ok
cmp [error_line],0
jne bswap_ok
mov eax,[current_line]
mov [error_line],eax
mov [error],invalid_use_of_symbol
bswap_ok:
cmp [value_size],2
je bswap_word
cmp [value_size],4
jne invalid_expression
bswap_dword:
mov eax,dword [edi]
bswap eax
mov dword [edi],eax
add edi,14h
jmp calculation_loop
bswap_word:
cmp dword [edi+4],0
jne bswap_dword
cmp word [edi+2],0
jne bswap_dword
mov ax,word [edi]
shl ax,16
bswap eax
mov word [edi],ax
add edi,14h
jmp calculation_loop

Lastly, in parser.inc, after db 3,'rva',0E0h (line 1005 ish), insert:
db 5,'bswap',0D2h

For rol and ror, in parser.inc after db 3,'shr',0C1h (line 994 ish) insert:

db 3,'rol',0C2h
db 3,'ror',0C3h

And in expressi.inc, after je calculate_shr (line 864) insert:

cmp al,0C2h
je calculate_rol
cmp al,0C3h
je calculate_ror

and after the shr_negative: block (line 1426 ish) insert:

calculate_rol:
mov eax,dword [edi+4]
bt eax,31
jc rol_negative
or eax,eax
jnz zero_value
mov ecx,[edi]
cmp ecx,64
jae zero_value
cmp ecx,32
jae rol_high
mov edx,[ebx+4]
mov eax,[ebx]
rol eax,cl
mov [ebx],eax
mov [ebx+4],edx
jmp calculation_loop
rol_high:
sub cl,32
mov eax,[ebx]
rol eax,cl
mov [ebx+4],eax
mov dword [ebx],0
jmp calculation_loop
rol_negative:
not dword [edi]
not dword [edi+4]
add dword [edi],1
adc dword [edi+4],0
calculate_ror:
mov eax,dword [edi+4]
bt eax,31
jc ror_negative
or eax,eax
jnz zero_value
mov ecx,[edi]
cmp ecx,64
jae zero_value
cmp ecx,32
jae ror_high
mov edx,[ebx+4]
mov eax,[ebx]
ror edx,cl
mov [ebx],eax
mov [ebx+4],edx
jmp calculation_loop
ror_high:
sub cl,32
mov eax,[ebx+4]
shr eax,cl
mov [ebx],eax
mov dword [ebx+4],0
jmp calculation_loop
ror_negative:
not dword [edi]
not dword [edi+4]
add dword [edi],1
adc dword [edi+4],0
jmp calculate_rol


Privalov might want to change my choice of C2,C3 and D2.
I tested this, and it seems ok, but I can confirm I really have no idea what I'm doing other than cribbing code from the closest match and tweaking it slightly.

Regards,
Pete
Post 08 May 2004, 17:59
View user's profile Send private message Visit poster's website Reply with quote
ShortCoder



Joined: 07 May 2004
Posts: 105
ShortCoder
vid wrote:
ShortCoder: no, it wouldn't be directive if it would work this way. a you said, "instruction, that is ...", that means directive have to be in place for instruction, not in instruction arguments. Instruction is first symbol (word) at the beggining of line or behind ":" of label definition. If it would be preprocessor directive (anyway, it would be preprocessor operator, not directive), it could work only with numeric constants, not with expressions or labels.


Let me clarify. By "instruction" I don't necessarily mean "assembly language instruction". I mean "anything which instructs a compiler/assembler/program what to do next". Yes, in this sense, a preprocessor operator would be a type of instruction (it instructs the preprocessor). And, I also think that it probably won't be added since it can easily be done another way.
Post 08 May 2004, 21:16
View user's profile Send private message Reply with quote
comrade



Joined: 16 Jun 2003
Posts: 1137
Location: Russian Federation
comrade
Yes, rol and ror would be nice too (officially)

_________________
comrade (comrade64@live.com; http://comrade.ownz.com/)
Post 09 May 2004, 01:01
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
comrade



Joined: 16 Jun 2003
Posts: 1137
Location: Russian Federation
comrade
vid wrote:
Also i don't believe privalov will add something that can be acheived another way


shr/shl can be achieved using macros too, yet they are internal assembler operators
using macro will require using another variable, while bswap/rol/ror operators could be used directly inside expression

_________________
comrade (comrade64@live.com; http://comrade.ownz.com/)
Post 09 May 2004, 01:02
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
ShortCoder: And I meant "directive" as defined internally in FASM - pseudo instruction that instructs compiler to do something (eg. something other than place instruction opcode into destination file)

OK, i personally think bswap wouldn't be very useful, but it also wouldn't do any bad, so let's privalov decide.
Post 09 May 2004, 11:11
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7734
Location: Kraków, Poland
Tomasz Grysztar
fasm performs calculations on 64-bit values - should bswap work only on lower 32-bit half? Or return an error if value hasn't got zeros in high 32 bits? And what about swapping bytes in a word?
Post 09 May 2004, 11:41
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
answer for last question is that it should swap bytes in word (like bswap instruction does). I think "value out of range" on value greater than 2^32 could cause problems when somebody wants to bswap negative value. Of course he can force value to 32 bits using dword directive, but i bet there will be few questions like:
why doesn't "mov ax,bswap -1" work?

bswap operator seems to be even more problematic than i first thought.
Post 09 May 2004, 13:08
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7734
Location: Kraków, Poland
Tomasz Grysztar
vid wrote:
answer for last question is that it should swap bytes in word (like bswap instruction does).

Not really - bswap 1234h would be 34210000h with the 32-bit operation.

But there's a solution - to make bswap work always on the intended value size, fasm already uses such predictions for logical operations. And so dw bswap 1234h would generate 3412h word value, dd bswap 1234h would generate 34120000h double word, etc.
Post 09 May 2004, 14:08
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
Yes, that would be correct. Negative values should be treated as unsigned values (e.g. bswap -1 would result in (extended)FFFFFFFFh). If size of value cannot be determined, is it possible to also use qword/dword/word/byte modifier in conjunction with bswap?

_________________
comrade (comrade64@live.com; http://comrade.ownz.com/)
Post 09 May 2004, 15:23
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7734
Location: Kraków, Poland
Tomasz Grysztar
Yes, this would be the same feature as is already used for the logical operations, though you may have never noticed it. Check this code for example:
Code:
qval = qword not 07Fh
dval = dword not 07Fh
bval = byte not 07Fh

dq qval
dq dval
dq bval    
Post 09 May 2004, 15:29
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7734
Location: Kraków, Poland
Tomasz Grysztar
Here's my quick implementation of such feature. It still may need some fixes, I'll have to review it a little when I have some more time.
Post 09 May 2004, 15: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
Thank you Privalov!
Seems to work fine, though a few warnings:
Code:
use32
x = bswap 1234h
mov eax, x    


error: value out of range. because x is 64-bit and is in actuality 3412000000000000h. Though this would work:
Code:
use32
x = bswap 1234h
mov eax, x shr 32    


Is there any way to do something like:
[code]mov eax, dword bswap word 1234h[/b] which would make eax equal to 00003412h? its not very important

Will rol/ror also be added?

_________________
comrade (comrade64@live.com; http://comrade.ownz.com/)
Post 09 May 2004, 17:05
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7734
Location: Kraków, Poland
Tomasz Grysztar
Define it like:
Code:
x = dword bswap 1234h    

(compare the above samples for logical operations)
Post 09 May 2004, 19:39
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
Okie. Have you thought about rol/ror? Would be useful too.

_________________
comrade (comrade64@live.com; http://comrade.ownz.com/)
Post 10 May 2004, 02:59
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number 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-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.