flat assembler
Message board for the users of flat assembler.
![]() Goto page 1, 2 Next |
Author |
|
wht36
Many thanks, just what I need for my byte coder!
|
|||
![]() |
|
revolution
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. |
|||
![]() |
|
baldr
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 ![]() |
|||
![]() |
|
LocoDelAssembly
Quote:
|
|||
![]() |
|
baldr
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 } ![]() |
|||
![]() |
|
LocoDelAssembly
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). ![]() |
|||
![]() |
|
wht36
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? |
|||
![]() |
|
baldr
wht36,
You're almost correct. Expressions are allowed even without (), but the result is rather unexpected (at a first glance). |
|||
![]() |
|
revolution
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 |
|||
![]() |
|
baldr
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 ![]() |
|||
![]() |
|
revolution
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. |
|||
![]() |
|
wht36
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 } |
|||
![]() |
|
Tomasz Grysztar
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) } |
|||
![]() |
|
baldr
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. ![]() |
|||
![]() |
|
baldr
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. ![]() |
|||
![]() |
|
Tomasz Grysztar
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. |
|||
![]() |
|
revolution
baldr wrote: LocoDelAssembly, Code: .: dd something |
|||
![]() |
|
wht36
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 }. |
|||
![]() |
|
baldr
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 ![]() 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 |
|||
![]() |
|
Goto page 1, 2 Next < Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.
Website powered by rwasa.