flat assembler
Message board for the users of flat assembler.
Index
> Macroinstructions > big endian macros Goto page 1, 2 Next |
Author |
|
wht36 08 Nov 2008, 16:14
Many thanks, just what I need for my byte coder!
|
|||
08 Nov 2008, 16:14 |
|
revolution 08 Nov 2008, 16:24
You may also want to test for overflow by checking that the high part of op is either all zeros (or all ones for signed values).
BTW: the "0 or " part is not needed. |
|||
08 Nov 2008, 16:24 |
|
baldr 08 Nov 2008, 16:58
Ralph,
Code: macro dw [val] { db (val) shr 8 and 0xFF, (val) and 0xFF } macro dd [val] { dw (val) shr 16 and 0xFFFF, (val) and 0xFFFF }; code reuse struc dw [val] { common dw val } struc dd [val] { common dd val } dw_big_endian: dw 0x1200+0x34, 0x5678 ; here you will see the purpose of () and [] dd_big_endian dd 0x9ABCDEF0 ; that's why duplicate macro with struc |
|||
08 Nov 2008, 16:58 |
|
LocoDelAssembly 08 Nov 2008, 17:48
Quote:
|
|||
08 Nov 2008, 17:48 |
|
baldr 08 Nov 2008, 19:44
LocoDelAssembly,
No need for that, label is defined even without using the dot. By the way, here is another variant: Code: macro big_endian datadef*, [tail] { common local ..start, ..datasize, ..dataptr, ..val1, ..val2 match datadef_directive val,datadef \{ virtual datadef_directive ? ..datasize = $-$$ end virtual ..start = $ if tail eq datadef else datadef, tail end if repeat ($ - ..start) / ..datasize ..dataptr = ..start + ..datasize*(%-1) repeat ..datasize/2 load ..val1 byte from ..dataptr + (%-1) load ..val2 byte from ..dataptr + ..datasize - % store byte ..val1 at ..dataptr + ..datasize - % store byte ..val2 at ..dataptr + (%-1) end repeat end repeat \} } struc big_endian datadef*, [val] { common big_endian datadef, val } repeat 5 big_endian dq 3 dup (%*25), ? end repeat rept 5 counter { here#counter big_endian dq 25*here#counter } |
|||
08 Nov 2008, 19:44 |
|
LocoDelAssembly 08 Nov 2008, 21:41
Quote:
And it is even said in the documentation but I don't know why I remember myself having problems for not defining the label ".:" (or using ". dX" on specialized cases like this one). |
|||
08 Nov 2008, 21:41 |
|
wht36 09 Nov 2008, 03:15
baldr wrote: Ralph, Hmm, so [] allows more than one arguments, and () allows expression such as (0x1200+0x34). And does the struc allows one to declare stuff without the colon? |
|||
09 Nov 2008, 03:15 |
|
baldr 09 Nov 2008, 08:09
wht36,
You're almost correct. Expressions are allowed even without (), but the result is rather unexpected (at a first glance). |
|||
09 Nov 2008, 08:09 |
|
revolution 09 Nov 2008, 08:31
The expressions are evaluated according the to normal rules after the parameter replacement has been applied.
Code: macro something parameter { dd parameter*4 } something 1 ;generates "dd 1*4" something 2+3 ;generates dd 2+3*4 |
|||
09 Nov 2008, 08:31 |
|
baldr 09 Nov 2008, 09:07
revolution,
By the way, what do you think about big-endian dsomethings? In my opinion, macro should have the same name as some built-in feature only if it behaves exactly the same in documented situations (I mean, same syntax and semantics, that's what I was trying to implement in big_endian macro), with exact indication of extended syntax/semantics (hence it's prefix macro). That's the example: Code: cmp [number], 0x78563412 ;dd is redefined somewhere above to big-endian dd number dd 0x12345678 Code: cmp [number], 0x78563412 ;big_endian defined, dd left alone number big_endian dd 0x12345678 |
|||
09 Nov 2008, 09:07 |
|
revolution 09 Nov 2008, 09:46
I would not personally use dd for a BE version of the normal dd. It would seem to me to be far too confusing and prone to errors.
The choice of name is arbitrary and is generally influenced by the authors own biases. So it really doesn't matter, but I am sure most would agree that names that appear more meaningful would be preferable to completely random names. Another option is to make separate macros for each size. be_dq, be_dd, be_dw or, depending upon one's personal preference dq_be, dd_be, dw_be. |
|||
09 Nov 2008, 09:46 |
|
wht36 09 Nov 2008, 11:40
While testing baldr's macros, I found that it does not accept floating point numbers (error: invalid value), and the problem appears to be due to shr. So is there a way to do it without using shr? I have tried using the other macro, but I think I'm not using it right (generate some strange output...). Nevertheless, I derived a rather ugly macro from it that works with floats as well.
Code: macro dd [op] { local .dd,.a,.b .dd: dd op load .a byte from .dd load .b byte from .dd+3 store byte .b at .dd store byte .a at .dd+3 load .a byte from .dd+1 load .b byte from .dd+2 store byte .b at .dd+1 store byte .a at .dd+2 } macro dw [op] { local .dw,.a,.b .dw: dw op load .a byte from .dw load .b byte from .dw+1 store byte .b at .dw store byte .a at .dw+1 } |
|||
09 Nov 2008, 11:40 |
|
Tomasz Grysztar 09 Nov 2008, 12:17
Simpler modification that fixes this problem:
Code: macro dd op { local value value = dword op dd 0 or ((value and 0FF000000h) shr 24) or \ ((value and 000FF0000h) shr 8) or \ ((value and 00000FF00h) shl 8) or \ ((value and 0000000FFh) shl 24) } |
|||
09 Nov 2008, 12:17 |
|
baldr 09 Nov 2008, 12:22
wht36,
For integral type, value and representation essentially the same, that's why you can simply split value in parts with shift/and, then combine them together in another order. Floating point values aren't so straightforward. See big_endian macro above, it's much more generic (but idea is the same as yours). What kind of strange output it generates? rept {} and repeat… end repeat are leftovers from test, you may safely delete them from source. |
|||
09 Nov 2008, 12:22 |
|
baldr 09 Nov 2008, 12:28
LocoDelAssembly,
I'd remembered why I didn't use "." in struc definition: that way you'll end up with data definition directive, not macro expansion. |
|||
09 Nov 2008, 12:28 |
|
Tomasz Grysztar 09 Nov 2008, 12:42
baldr wrote: I'd remembered why I didn't use "." in struc definition: that way you'll end up with data definition directive, not macro expansion. You should have used it this way: Code: label . dword By not using "." you cause the size-less label to be generated. |
|||
09 Nov 2008, 12:42 |
|
revolution 09 Nov 2008, 13:11
baldr wrote: LocoDelAssembly, Code: .: dd something |
|||
09 Nov 2008, 13:11 |
|
wht36 09 Nov 2008, 13:12
Tomasz Grysztar wrote: Simpler modification that fixes this problem: Ah... typecasting. Of course! baldr wrote: For integral type, value and representation essentially the same, that's why you can simply split value in parts with shift/and, then combine them together in another order. Floating point values aren't so straightforward. I am not sure but the output is expanded with zeroes. But perhaps it's because I used Code: macro dd [op] { big_endian dd,op }. |
|||
09 Nov 2008, 13:12 |
|
baldr 09 Nov 2008, 13:26
Tomasz Grysztar,
Is there a reason for that Code: fp_d = dword 1.0 dd fp_d Code: dd dword 1.0 I'm trying to devise some evil scheme to pass struc big_endian's label to macro big_endian, of no avail yet. struc and dds are so much context-sensitive… revolution, That label will be typeless, as Tomasz noted. wht36, Comma between dd and [op] is the source of problem. match datadef_directive val,dd will not match and macro will expand to nothing. I didn't comment macro usage, my fault. It's used as prefix for data definition directive, like Code: big_endian dd 1.0; same effect as db 0x3F, 0x80, 0, 0 |
|||
09 Nov 2008, 13:26 |
|
Goto page 1, 2 Next < Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.