Author Topic: What am I doing wrong in the creation and use of this macro?  (Read 1944 times)

jj2007

  • Member
  • *****
  • Posts: 13957
  • Assembly is fun ;-)
    • MasmBasic
Re: What am I doing wrong in the creation and use of this macro?
« Reply #15 on: June 28, 2022, 11:10:02 AM »
Once again:  I DID post complete code.  I DID test the code I posted.  There IS NO OTHER CODE.  The only error I got during assembly of the code that I posted was "syntax error: SUBSTR".  There were NO OTHER ERRORs.  And getting that ONE error was WHY I made my original post.  Why you got a bunch of errors that I did NOT get is unknown to me.  I did not get those errors.

OK, mystery solved - my apologies: you did not get those errors because you are using a very recent version of ML.exe which, apparently, does no longer need the usual .686p at the start of the file. That makes your code incompatible with the setups of most but not all members here. The Masm32 SDK comes with ML version 6.14; all MASM versions up to version 9.0 need the .*86, same for the Watcom gang.

My advice: add a .686p on top of all your sources. Visual Crap may not need it, but you can spare yourself such negative experiences.

And keep in mind that you cannot change a macro argument, as explained earlier. It will choke.

Technically speaking, you can change a proc argument, though:

Code: [Select]
MyTest proc zStringPtr:DWORD
  mov zStringPtr, 123

However, you should never do that, it's considered bad and risky coding.

Rav

  • Regular Member
  • *
  • Posts: 36
Re: What am I doing wrong in the creation and use of this macro?
« Reply #16 on: June 28, 2022, 11:24:35 AM »
Once again:  I DID post complete code.  I DID test the code I posted.  There IS NO OTHER CODE.  The only error I got during assembly of the code that I posted was "syntax error: SUBSTR".  There were NO OTHER ERRORs.  And getting that ONE error was WHY I made my original post.  Why you got a bunch of errors that I did NOT get is unknown to me.  I did not get those errors.

OK, mystery solved - my apologies: you did not get those errors because you are using a very recent version of ML.exe which, apparently, does no longer need the usual .686p at the start of the file. That makes your code incompatible with the setups of most but not all members here. The Masm32 SDK comes with ML version 6.14; all MASM versions up to version 9.0 need the .*86, same for the Watcom gang.

My advice: add a .686p on top of all your sources. Visual Crap may not need it, but you can spare yourself such negative experiences.

And keep in mind that you cannot change a macro argument, as explained earlier. It will choke.

Technically speaking, you can change a proc argument, though:

Code: [Select]
MyTest proc zStringPtr:DWORD
  mov zStringPtr, 123

However, you should never do that, it's considered bad and risky coding.

Thanks for saying that -- I appreciate it.  And thanks for the additional info.

NoCforMe

  • Member
  • *****
  • Posts: 1124
Re: What am I doing wrong in the creation and use of this macro?
« Reply #17 on: June 28, 2022, 11:34:50 AM »
Technically speaking, you can change a proc argument, though:

Code: [Select]
MyTest proc zStringPtr:DWORD
  mov zStringPtr, 123

However, you should never do that, it's considered bad and risky coding.

Just to address this small issue, was reading about this the other day and the writer said that procedure arguments are basically the same as local variables; they're all on the stack, it's just that arguments get set by the caller. So no harm, no foul if you change an argument--just so long as you're sure you're never going to need it again, 'cause you can't get it back once you've overwritten it. Amiright?

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 10583
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: What am I doing wrong in the creation and use of this macro?
« Reply #18 on: June 28, 2022, 01:11:35 PM »
Rav,

Macros have their uses if you are used to writing them but stripping leading blanks or tabs from text is best done with a tiny loop where you start at the beginning of the string and scan through the start of the string until the next character is neither a space (ascii 32) or a tab (ascii 9). Code of this type is both tiny and fast.

    mov ecx, pstr                       ; load the string
    sub ecx, 1                          ; set up for loop

  @@:
    add ecx, 1
    cmp BYTE PTR [ecx], 32              ; test for space
    jne @F
    cmp BYTE PTR [ecx], 9               ; test for tab
    jne @B

  @@:
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 10583
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: What am I doing wrong in the creation and use of this macro?
« Reply #19 on: June 28, 2022, 01:27:47 PM »
Here is the complete example.


; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    include \masm32\include\masm32rt.inc
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

comment * -----------------------------------------------------
                        Build this  template with
                       "CONSOLE ASSEMBLE AND LINK"
        ----------------------------------------------------- *

    sptrim PROTO sptr:DWORD,buffr:DWORD


    .data
      MyString db "        12345678",0      ; the string
      pstr dd MyString                      ; its pointer
      buffer db "                         " ; buffer
      pbuf dd buffer                        ; its pointer

    .code

start:
   
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    call main
    inkey
    exit

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

main proc

    LOCAL pout :DWORD

    print pstr,13,10

    invoke sptrim,pstr,pbuf
    mov pout, eax

    print pout, 13, 10

    ret

main endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

sptrim proc sptr:DWORD,buffr:DWORD

    mov ecx, sptr                       ; load the string
    sub ecx, 1                          ; set up for loop

  @@:
    add ecx, 1
    cmp BYTE PTR [ecx], 32              ; test for space
    jne @F
    cmp BYTE PTR [ecx], 9               ; test for tab
    jne @B

  @@:
    mov edx, buffr
    sub ecx, 1
    sub edx, 1

  @@:
    add ecx, 1
    add edx, 1
    mov al, BYTE PTR [ecx]
    mov BYTE PTR [edx], al
    test al, al
    jnz @B

    mov eax, buffr

    ret 8

sptrim endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

end start
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy: