News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

How can I use @ProcName ?

Started by mineiro, February 19, 2023, 10:35:28 PM

Previous topic - Next topic

mineiro

sirs;
How can I use @ProcName? Any example?
Thanks.
I'd rather be this ambulant metamorphosis than to have that old opinion about everything


LiaoMi

Quote from: mineiro on February 19, 2023, 10:35:28 PM
sirs;
How can I use @ProcName? Any example?
Thanks.

ChatGPT  :biggrin:  :biggrin:  :biggrin:

In Microsoft Macro Assembler (MASM), @ProcName is a predefined symbol that can be used to reference the name of the current procedure or function being assembled. Here's an example of how to use @ProcName in MASM:
MyProc PROC
    push ebp
    mov ebp, esp

    ; Get the name of the current procedure
    mov edx, OFFSET @ProcName

    ; Print the procedure name to the console
    push OFFSET format_string
    push edx
    call printf

    mov esp, ebp
    pop ebp
    ret

format_string db "%s",0

MyProc ENDP


In the above example, the MyProc procedure gets the name of the current procedure using the @ProcName symbol and prints it to the console using the printf function. Note that @ProcName is an assembler directive and should be used with the OFFSET operator to get the address of the symbol.

When assembled and linked with a C runtime library that provides printf, this program should print the name of the MyProc procedure to the console when executed.


In Microsoft Macro Assembler (MASM), @ProcName can be used for debugging purposes to help identify the name of the current procedure or function being executed. Here's an example of how to use @ProcName for debugging in MASM:
.MODEL flat, stdcall
OPTION casemap:none

MyProc PROC
    push ebp
    mov ebp, esp

    ; Get the name of the current procedure
    mov edx, OFFSET @ProcName

    ; Print the procedure name to the debugger
    int 3 ; Breakpoint to launch the debugger
    ; Note: the debugger should display the procedure name in the disassembly view

    mov esp, ebp
    pop ebp
    ret
END MyProc


In the above example, the MyProc procedure gets the name of the current procedure using the @ProcName symbol and prints it to the debugger using the int 3 instruction, which generates a breakpoint interrupt that launches the debugger. When the debugger is launched, it should display the name of the current procedure in the disassembly view.

This technique can be useful for debugging complex programs with many procedures or functions, as it allows you to quickly identify which procedure or function is being executed at a given point in the program.

Note that the int 3 instruction should be used with caution, as it can cause the program to crash if there is no debugger attached. It is recommended to use this technique only during debugging and to remove the int 3 instruction once the debugging is complete.


In Microsoft Macro Assembler (MASM), @ProcName can be used for various purposes, such as generating code that depends on the name of the current procedure, or for generating error messages or logging information that includes the name of the current procedure. Here are some additional examples of how to use @ProcName in MASM:
; Example 1: Generate code based on the name of the current procedure
MyProc PROC
    push ebp
    mov ebp, esp

    ; Generate code based on the name of the current procedure
    cmp OFFSET @ProcName, OFFSET MyProc
    je MyProcIsCurrent

    ; ...

MyProcIsCurrent:
    ; ...

    mov esp, ebp
    pop ebp
    ret
END MyProc


In the above example, the MyProc procedure generates different code depending on whether it is the current procedure or not. This could be useful for implementing conditional logic or for optimizing code based on the name of the current procedure.

; Example 2: Generate an error message that includes the name of the current procedure
MyProc PROC
    push ebp
    mov ebp, esp

    ; Generate an error message that includes the name of the current procedure
    push OFFSET error_message
    push OFFSET @ProcName
    call ShowErrorMessage

    ; ...

MyProcCleanup:
    ; ...

    mov esp, ebp
    pop ebp
    ret

error_message db "Error in procedure %s",0
END MyProc


In the above example, the MyProc procedure generates an error message that includes the name of the current procedure using the @ProcName symbol and passes it to a function called ShowErrorMessage. This could be useful for debugging or for providing more informative error messages to users.

; Example 3: Log information that includes the name of the current procedure
MyProc PROC
    push ebp
    mov ebp, esp

    ; Log information that includes the name of the current procedure
    push OFFSET log_message
    push OFFSET @ProcName
    call LogMessage

    ; ...

MyProcCleanup:
    ; ...

    mov esp, ebp
    pop ebp
    ret

log_message db "Procedure %s completed successfully",0
END MyProc


In the above example, the MyProc procedure logs information that includes the name of the current procedure using the @ProcName symbol and passes it to a function called LogMessage. This could be useful for debugging or for providing a record of which procedures were executed during the program's operation.

In addition to using @ProcName for debugging purposes or generating code based on the name of the current procedure, Microsoft Macro Assembler (MASM) provides several other uses for @ProcName. Here are a few examples:
; Example 1: Use @ProcName in a conditional statement to test if a procedure is defined
IFDEF MyProc
    ; MyProc is defined
ELSE
    ; MyProc is not defined
ENDIF

; Example 2: Use @ProcName to define a label based on the name of the current procedure
MyProc PROC
    jmp @LabelName
    ; ...

@LabelName:
    ; ...

    ret
END MyProc

; Example 3: Use @ProcName to initialize data based on the name of the current procedure
MyProc PROC
    push ebp
    mov ebp, esp

    ; Initialize data based on the name of the current procedure
    lea eax, OFFSET @ProcName
    mov ebx, OFFSET data_segment
    mov [ebx], eax

    ; ...

    mov esp, ebp
    pop ebp
    ret

data_segment db "Data initialized by procedure: ",0,0,0,0,0,0,0,0,0
END MyProc


In the first example, @ProcName is used in a conditional statement to test if a procedure called MyProc is defined. This could be useful for controlling the behavior of the program based on whether or not a particular procedure is present.

In the second example, @ProcName is used to define a label based on the name of the current procedure. This could be useful for generating code that depends on the name of the current procedure, such as for implementing a jump table.

In the third example, @ProcName is used to initialize data based on the name of the current procedure. This could be useful for generating data structures that depend on the name of the current procedure, such as for creating a call stack or tracking the execution history of the program.

MyProcName db ?

MyProc PROC
    push ebp
    mov ebp, esp

    ; Initialize a global variable with the name of the current procedure
    lea eax, OFFSET @ProcName
    mov ebx, OFFSET MyProcName
    mov [ebx], eax

    ; ...

    mov esp, ebp
    pop ebp
    ret
END MyProc

; Access the global variable outside of a procedure
mov eax, OFFSET MyProcName
; ...


MyProc PROC
    ; ...
    cmp eax, ebx
    jne Error
    ; ...
    ret

Error:
    lea eax, OFFSET @ProcName
    invoke MessageBox, NULL, ADDR eax, NULL, MB_OK
    ; ...
    ret
END MyProc


MyProc PROC
    lea eax, OFFSET @ProcName
    ; ...
    .if (@ProcName == MyProc)
        ; Generate code specific to the MyProc procedure
        ; ...
    .elseif (@ProcName == AnotherProc)
        ; Generate code specific to the AnotherProc procedure
        ; ...
    .endif
    ; ...
END MyProc


.DATA
MyDataStruct STRUCT
    Name DWORD ?
    ; ... other fields go here ...
MyDataStruct ENDS
.CODE
MyProc PROC
    push ebp
    mov ebp, esp
    ; ... procedure code goes here ...
    mov eax, OFFSET MyDataStruct
    mov DWORD PTR [eax], OFFSET @ProcName
    ; ... more data structure code goes here ...
    pop ebp
    ret
ENDP


MyProc PROC
    @ProcName MyProc
    MyLabel_@ProcName:
        ; code that references the label MyLabel_MyProc
    ; procedure code goes here
MyProc ENDP

MyOtherProc PROC
    @ProcName MyOtherProc
    MyLabel_@ProcName:
        ; code that references the label MyLabel_MyOtherProc
    ; procedure code goes here
MyOtherProc ENDP

mineiro

Thank you sir  mabdelouahab , thank you sir LiaoMi.
Sorry for delay in answer, I was doing some tests before post this message.

The code below work with ml v8

;ml64 /c procname.asm
.data
.code
public WinMainCrt
WinMainCrt::
@ProcName textequ <WinMainCrt>
%echo @ProcName
@ProcName textequ <mineiro>
%echo @ProcName
ret
end


Output:

C:\masm64\bin64>ml64 /c procname.asm
Microsoft (R) Macro Assembler (AMD64) Version 8.00.40310.39
Copyright (C) Microsoft Corporation.  All rights reserved.

Assembling: procname.asm
WinMainCrt
mineiro


But I'm not being able to make this work in linux with uasm.

;uasm -elf64 procname.uasm
.X64
exit PROTO :VARARG
.data
.code
public main
main proc
%echo @ProcName                 ;@ProcName
;@ProcName textequ <main>       ;Error A2143: Symbol redefinition: @ProcName
;%@ProcName textequ <main>      ;Error A2143: Symbol redefinition: @ProcName
;myproc textequ %<@ProcName>    ;Error A2167: Missing quotation mark in string
myproc textequ %@ProcName
;%myproc textequ <@ProcName>
%echo myproc                    ;0
;main textequ %@ProcName
;@ProcName CATSTR <main>
;@ProcName textequ <mineiro>
%echo @ProcName                 ;
invoke exit,0
main endp
end


Output:

@ProcName
0
@ProcName
procname.uasm: 21 lines, 3 passes, 2649 ms, 0 warnings, 0 errors

I'd rather be this ambulant metamorphosis than to have that old opinion about everything

Vortex

It looks like we need to create a macro for the 32-bit version of ml.exe

jj2007

Quote from: Vortex on March 09, 2023, 03:51:02 AM
It looks like we need to create a macro for the 32-bit version of ml.exe

I have a ProcName$() macro but it requires a custom prolog.

HSE

Quote from: LiaoMi on March 07, 2023, 11:08:59 AM

ChatGPT  :biggrin:  :biggrin:  :biggrin:

In Microsoft Macro Assembler (MASM), @ProcName is a predefined symbol


Hello LiaoGPT!

What version you used to test that?

(I hope you don't believe what bot say  :biggrin: :biggrin:)
Equations in Assembly: SmplMath

HSE

Quote from: mineiro on March 08, 2023, 01:56:00 PM
The code below work with ml v8

That is a simple equate, you are not using any @ProcName directive.
Equations in Assembly: SmplMath

mineiro

Quote from: HSE on March 09, 2023, 07:01:48 AM
That is a simple equate, you are not using any @ProcName directive.
Yes, so how can I use @ProcName directive? Any example?

I tried Liaomi examples, that return to me some values not expected. When I tried to load @ProcName and main offset to registers just to compare if both are equals, the result was not equal. When I tried to %echo @ProcName, nothing happen. I got a lot of redefinition not allowed. So, ... .
Instead of use @ProcName I can use other variable name to work like in a ml64 example.

The main objective is to create a macro. In our source code we know the procedures name, but in others source code we don't know procedures name.
I'd rather be this ambulant metamorphosis than to have that old opinion about everything

mineiro

I forgot to say the real purpose;
I was reading dwarf debug information to elf files, and I need all variable names, (including locals) not just procedure name, macros name, line numbers in source code where they are referencied, ... .
Well, for a while I'm just reading specification, I don't have tried to do a simple handmade source code.
I'd rather be this ambulant metamorphosis than to have that old opinion about everything

jj2007

Quote from: mineiro on March 09, 2023, 09:01:30 PM
Quote from: HSE on March 09, 2023, 07:01:48 AM
That is a simple equate, you are not using any @ProcName directive.
Yes, so how can I use @ProcName directive? Any example?

@ProcName seems not to be working. As mentioned above, it can be done with a custom prolog macro. This works fine:

include \Masm32\MasmBasic\Res\JBasic.inc ; ## console demo, builds in 32- or 64-bit mode with UAsm, AsmC and ML ##
.code
DefProc SayHi proc argString:SIZE_P, argDword, argDouble:REAL8
Local v1, v2:REAL8, rc:RECT
  PrintLine "We are in ", ProcName$()
  ret
SayHi endp
SomeTest proc arg
  PrintLine "We are in ", ProcName$()
  ret
SomeTest endp
Init ; OPT_64 1 ; put 0 for 32 bit, 1 for 64 bit assembly
  Cls 8
  PrintLine Chr$("This program was assembled with ", @AsmUsed$(1), " in ", jbit$, "-bit format.")
  jinvoke SomeTest, 123h
  jinvoke SayHi, Chr$("Argument #1 is a string"), 222222222, FP8(33333.33333)
  MsgBox 0, ProcName$(), "The name of the current proc:", MB_OK
EndOfCode


Output:
This program was assembled with UAsm64 in 64-bit format.
We are in SomeTest
We are in SayHi
(MsgBox:) The name of the current proc: Main

Works with MASM, UAsm and AsmC :cool:

HSE

Quote from: mineiro on March 09, 2023, 09:01:30 PM

Yes, so how can I use @ProcName directive? Any example?


There is no @ProcName symbol in MASM.  :biggrin:  (Could be in 16 bits era, I don't know)

Johnsa maked something in UASM but I never used.
Equations in Assembly: SmplMath

jj2007

Quote from: HSE on March 09, 2023, 10:37:03 PMJohnsa maked something in UASM but I never used.

The point is that it apparently doesn't exist.

  % echo ProcName
  % echo @ProcName
  .err

HSE

Quote from: johnsa on January 08, 2018, 03:20:23 AM
@ProcName is in,  the stack alignment issue is sorted and a bunch of other pending changes. 2.46.6 packages are up :)

But look like function was removed:

Code (UASM 2.55) Select
/* table of predefined numeric equates */
static const struct eqitem eqtab[] = {
    { "__HJWASM__", _UASM_VERSION_INT_, NULL, NULL },
{ "__UASM__", _UASM_VERSION_INT_, NULL, NULL },
{ "__JWASM__", 212, NULL, NULL },
    { "$",         0,                   UpdateCurPC, &symPC },
    { "@Line",     0,                   UpdateLineNumber, &LineCur },
{ "@ProcLine", 0,                   NULL, NULL },
{ "@Arch",     ARCH_SSE,            NULL, NULL },
{ "@Platform", 0,                   NULL, NULL },
{ "@LastReturnType", 0,             NULL, NULL },
{ "@ProcName", 0,                   NULL, NULL },
    { "@WordSize", 0,                   UpdateWordSize, NULL }, /* must be last (see SymInit()) */
};
Equations in Assembly: SmplMath

mineiro

Thanks for answers sir HSE and sir jj2007.
That @ProcName is in documentation. So, sounds that continue being a reserved word.

Well, maybe creating a macro to everything will be a pain, source code will look ugly.
Again, thank sirs.
I'd rather be this ambulant metamorphosis than to have that old opinion about everything