flat assembler
Message board for the users of flat assembler.

flat assembler > Macroinstructions > QUESTION : How can I make this macro work?

Author
Thread Post new topic Reply to topic
xanatose



Joined: 09 Jan 2004
Posts: 57
After reading and modifitying it little more I still could not figure what I am doing wrong.

The 2 macros are:

Code:
;====================================================
; Creates a constant string
;
; This will create both a label to the string and a variable szName.size for its size.
;
; Must be used in the constant section.
;
; The size of the string does not include the NUL character at the end of it,
;
; @param szName Name of the label of the string
; @param szString String
macro cstring szName,[szString] {
  common label szName
  forward db szString
  lenof.#szName equ $ - szName
  db 0
}

;====================================================
; Copies a constant string to an area of Memory
;
; @param szName label of the constant string
; @param edi points to destination
; @affects esi edi ecx
; @note User is reponsible of adding the NUL character at the end of the string
macro cstring_copy szName {
  mov esi,szName

  if (lenof.szName) > 4
    mov ecx,(lenof.szName) / 4
    rep movsd
  end if

  if ((lenof.szName) and 3) > 0
    mov ecx,(lenof.szName) and 3
    rep movsb
  end if
}
    


It tells me that the symbol lenof.szName is undefined. Funny thing is that if I used outside a macro it works.
Code:

; In data
cstring szTest, "This is a test"

; Later in code
  mov ecx,(lenof.szTest) /4
  rep movsd

  mov ecx,(lenof.szTest) and 3
  rep movsb
    


works outside a macro, but inside one the len is undefined. How can I work around this?
Post 24 Jun 2005, 23:29
View user's profile Send private message Reply with quote
xanatose



Joined: 09 Jan 2004
Posts: 57
Found the error. But could not find a way to delete the post.

I was using lenof.szName instead of lenof.#szLen

For the curious the code:
Code:
;====================================================
; Creates a constant string
;
; This will create both a label to the string and a variable szName.size for its size.
;
; Must be used in the constant section.
;
; The size of the string does not include the NUL character at the end of it,
;
; @param szName Name of the label of the string
; @param szString String
macro cstring szName,[szString] {
  common label szName
  forward db szString
  lenof.#szName equ $ - szName
  db 0
}

;====================================================
; Copies a constant string to an area of Memory
;
; @param szText label of the constant string
; @param edi points to destination
; @affects esi edi ecx
; @note User is reponsible of adding the NUL character at the end of the string
macro cstring_cpy szText {
  local szLen
  local bRemain
  local nDwords

  szLen = lenof.#szText ; Length of string

  ; Do we need to do anything at all?
  if szLen > 0
    ; Yes, if we are here.
    mov esi,szText   ; Set pointer

    ; Number of dwords to move
    nDwords = szLen / 4

    ; we use rep movsd if we have at least 4 dwords
    if nDwords > 3
      mov ecx,szLen / 4    ; Number of dwords to move
      rep movsd                         ; move them
    else if nDwords > 2
      ; 3 dwords
      movsd
      movsd
      movsd
    else if nDwords > 1
      ; 2 dwords
      movsd
      movsd
    else if nDwords > 0
      ; 1 dword
      movsd
    end if

    bRemain = szLen and 3 ; Remaining bytes

    ; Do we need to move 3 bytes?
    if bRemain > 2
      ; Move 3 bytes
      movsw
      movsb
    else if bRemain > 1   ; Do we need to move 2 bytes?
      ; Move 2 bytes
      movsw
    else if bRemain > 0  ; Do we need to move 1 byte?
      ; Move 1 byte
      movsb
    end if
  end if
}
    
Post 25 Jun 2005, 04:24
View user's profile Send private message Reply with quote
madmatt



Joined: 07 Oct 2003
Posts: 1046
Location: Michigan, USA
Hi, xanatose
I'm not an expert on Fasm marcros, but I think this line in your macro code:
Code:
lenof.#szName equ $ - szName    

Should be
Code:
lenof#.#szName equ $ - szName    

I think it will work like you want it to then.
Post 25 Jun 2005, 05:55
View user's profile Send private message Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 7238
Location: Kraków, Poland
madmatt: this second hash doesn't matter here.
Post 25 Jun 2005, 10:04
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< 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-2019, Tomasz Grysztar.

Powered by rwasa.