Current assembly position in an assembly-time variable? 
Author Message
 Current assembly position in an assembly-time variable?

In NASM, how do you assign the current assembly position to
an assembly-time variarble?

Say you want to create a linked list at assembly time.  The
following is the idea (but won't work because EQUs are
constants):

; A linked list of strings using assembly-time variables:

prev_addr EQU 0
...stuff

; Fisrt item in list:
this_addr EQU $-$$
dw prev_addr
prev_addr EQU this_addr
db 'str1'

: Second item in list:
this_addr equ $-$$
dw prev_addr
prev_addr equ this_addr
db 'str2'

; Third item in list:
this_addr equ $-$$
dw prev_addr
prev_addr equ this_addr
db 'str3'

; End of list

And trying to use "%assign $-$$" in place of the EQU
is an "expression syntax error".

I'm trying to do more than just create a linked list,
so I need a general answer on how-to assign the current
assembly position to an assembly-time variable, with the
ability to enclose it all in a macro.

Thanks.

SRB



Thu, 14 Oct 2004 01:29:49 GMT  
 Current assembly position in an assembly-time variable?

Quote:

> In NASM, how do you assign the current assembly position to
> an assembly-time variarble?

> Say you want to create a linked list at assembly time.  The
> following is the idea (but won't work because EQUs are
> constants):

> ; A linked list of strings using assembly-time variables:

> prev_addr EQU 0
> ...stuff

> ; Fisrt item in list:
> this_addr EQU $-$$
> dw prev_addr
> prev_addr EQU this_addr
> db 'str1'

>: Second item in list:
> this_addr equ $-$$
> dw prev_addr
> prev_addr equ this_addr
> db 'str2'

> ; Third item in list:
> this_addr equ $-$$
> dw prev_addr
> prev_addr equ this_addr
> db 'str3'

> ; End of list

> And trying to use "%assign $-$$" in place of the EQU
> is an "expression syntax error".

> I'm trying to do more than just create a linked list,
> so I need a general answer on how-to assign the current
> assembly position to an assembly-time variable, with the
> ability to enclose it all in a macro.

I think you're going to have trouble using "$-$$" as an operand to a
macro operator, since the macro-processing goes on in the preprocessor,
and I don't think the
preprocessor knows where "$" is.

You should be able to do,

some_addr dw $-$$ ; (or dd for 32 bit code)

And perhaps you could use Nasm's local label mechanism to your
advantage,

.some_addr dw $-$$

This would need to be associated with a "non-local" label, for each
occurance. Nasm will generate unique labels for each instance of a
macro, but I don't quite know how you're going to refer to them.

I'm afraid I'm not very experienced with Nasm's macro-capabilities. They
exist. Some say they're not very powerful. It *may* be that you've hit
upon something that Nasm isn't powerful enough to do, but don't give up
on it just because I don't know the answer. Maybe someone more
experienced with Nasm's macros can help you more.

Ruud posted some cute linked-list code using xor, a while ago. I don't
know if it would be suitable to your purposes - I can send it to you if
you want.

Best,
Frank



Thu, 14 Oct 2004 20:55:55 GMT  
 Current assembly position in an assembly-time variable?

Quote:


>>In NASM, how do you assign the current assembly position to
>>an assembly-time variarble?

>>Say you want to create a linked list at assembly time.  The
>>following is the idea (but won't work because EQUs are
>>constants):

>>; A linked list of strings using assembly-time variables:

>>prev_addr EQU 0
>>...stuff

>>; Fisrt item in list:
>>this_addr EQU $-$$
>>dw prev_addr
>>prev_addr EQU this_addr
>>db 'str1'

>>: Second item in list:
>>this_addr equ $-$$
>>dw prev_addr
>>prev_addr equ this_addr
>>db 'str2'

>>; Third item in list:
>>this_addr equ $-$$
>>dw prev_addr
>>prev_addr equ this_addr
>>db 'str3'

>>; End of list

>>And trying to use "%assign $-$$" in place of the EQU
>>is an "expression syntax error".

>>I'm trying to do more than just create a linked list,
>>so I need a general answer on how-to assign the current
>>assembly position to an assembly-time variable, with the
>>ability to enclose it all in a macro.

> I think you're going to have trouble using "$-$$" as an operand to a
> macro operator, since the macro-processing goes on in the preprocessor,
> and I don't think the
> preprocessor knows where "$" is.

> You should be able to do,

> some_addr dw $-$$ ; (or dd for 32 bit code)

> And perhaps you could use Nasm's local label mechanism to your
> advantage,

> .some_addr dw $-$$

> This would need to be associated with a "non-local" label, for each
> occurance. Nasm will generate unique labels for each instance of a
> macro, but I don't quite know how you're going to refer to them.

> I'm afraid I'm not very experienced with Nasm's macro-capabilities. They
> exist. Some say they're not very powerful. It *may* be that you've hit
> upon something that Nasm isn't powerful enough to do, but don't give up
> on it just because I don't know the answer. Maybe someone more
> experienced with Nasm's macros can help you more.

> Ruud posted some cute linked-list code using xor, a while ago. I don't
> know if it would be suitable to your purposes - I can send it to you if
> you want.

> Best,
> Frank

this is exactly what my forth compiler does.  all word headers are
linked by doing the following

%xdefine last 0   ;previous word in chain

%macro link_foo 0
%%link:
   dd last                           ;link to previous item(word ?)
   %xxdefine last %%link                    ;update last for next go
   more stuff here                   ;what ever you need
%endmacro

if you would like to see working code check out

<shameless plug> http://isforth.clss.net  </shameless plug>

which is my x86 only, linux only, direct threaded forth compiler that
uses only syscalls.  the kernel is written in 100% pure assembler using
NASM.

Im currently working on making nasm chain words on different linked
lists (called vocabularies) by modifying the existing macros.



Fri, 15 Oct 2004 06:53:03 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Metacompile assembly source to assembly source ?

2. Assembly Language for TI-86 or the Z80 assembly Language

3. Doing assembly and really doing assembly

4. Doing assembly and really doing assembly

5. problem porting assembly codes from ms inline assembly to linux (gnu - AT&T syntax)

6. AT&T assembly format to Intel assembly format

7. RISC Assembly, 386 Assembly

8. Any X86 Assembly language telecommute positions out there ?

9. Any X86 Assembly language telecommute positions out there ?

10. Counting bytes and assembly time stdout output

11. killing time in 6502 assembly

12. First time Assembly programmer seeks help...

 

 
Powered by phpBB® Forum Software