Author Topic: Question about TYPEDEF in masm  (Read 345 times)

StillLearningMasm

  • Regular Member
  • *
  • Posts: 6
Question about TYPEDEF in masm
« on: April 13, 2019, 07:48:31 PM »
To whom it may concern,

I read the documentation that came with my version of masm. It shows some examples for using TYPEDEF. After looking at the BNF Grammer that shows how masm sees the different instructions. I
created the following 2 instructions that assembled with no errors but I have found no examples on how to use them. Here are the 2 different examples that I am talking about:

label1 typedef ptr proc
label2 typedef proc

Does anybody have any examples on how to use them?
Thank you for your help.


jj2007

  • Member
  • *****
  • Posts: 9278
  • Assembler is fun ;-)
    • MasmBasic
Re: Question about TYPEDEF in masm
« Reply #1 on: April 13, 2019, 08:18:25 PM »
Welcome to the Forum :icon14:

I have no example ready, but you may find some stuff using the search function. Use e.g.  "typedef ptr proc" to find quite a number of threads. If that's not enough, try to search the old forum.

nidud

  • Member
  • *****
  • Posts: 1678
    • https://github.com/nidud/asmc
Re: Question about TYPEDEF in masm
« Reply #2 on: April 13, 2019, 09:23:12 PM »
Code: [Select]
    .486
    .model flat, c

    tfprintf    typedef proto :ptr sbyte, :vararg
    pfprintf    typedef ptr tfprintf

    .data
    printf      pfprintf 0
    format      db "address of printf: %p",10,0

    .code

main proc

    invoke printf, addr format, printf
    ret

main endp

    includelib \masm32\lib\kernel32.lib

    .data
    cmsvcrt     db "msvcrt",0
    cprintf     db "printf",0

    .code

LoadLibraryA    proto stdcall :ptr sbyte
GetProcAddress  proto stdcall :dword, :ptr sbyte
ExitProcess     proto stdcall :dword

start:
    invoke LoadLibraryA, addr cmsvcrt
    invoke GetProcAddress, eax, addr cprintf
    mov printf,eax
    invoke main
    invoke ExitProcess,0

    end start

StillLearningMasm

  • Regular Member
  • *
  • Posts: 6
Re: Question about TYPEDEF in masm
« Reply #3 on: April 14, 2019, 04:14:49 AM »
Thank you for your example jj2007 but the example I am looking for are using these instructions:

label1 typedef ptr proc
label2 typedef proc

I am looking for examples of using user defined proc. I have not found any.

StillLearningMasm

  • Regular Member
  • *
  • Posts: 6
Re: Question about TYPEDEF in masm
« Reply #4 on: April 14, 2019, 04:20:22 AM »
Do these statements create the same user defined type:

Label1 typedef
Label1 typedef ptr

I did not find and examples of using the first statement. Masm gave me no error for either one of the statements. I did find a example of using the second statement. Should the first statement be a error?

jj2007

  • Member
  • *****
  • Posts: 9278
  • Assembler is fun ;-)
    • MasmBasic
Re: Question about TYPEDEF in masm
« Reply #5 on: April 14, 2019, 12:15:31 PM »
No idea. It is difficult to understand out of context what you mean, and we rarely use these constructs, probably because there are better alternatives. Can you post a complete example showing where it chokes, and explaining what you really want to achieve?

AW

  • Member
  • *****
  • Posts: 1873
  • Let's Make ASM Great Again!
Re: Question about TYPEDEF in masm
« Reply #6 on: April 14, 2019, 05:01:07 PM »
All 6 variations are the same.
(Do you mean ?
label1 typedef PROTO
label2 typedef ptr label1)

Code: [Select]
.486

.model flat, stdcall

printf proto C :ptr, :vararg
ExitProcess proto :dword

label1 typedef ptr proc
label2 typedef proc

.data
msg db "Hello from called function",10,0

.code

myFunc proc
invoke printf, addr msg
ret
myFunc endp

main proc
mov esi, myFunc
call esi
call label1 ptr esi
call label2 ptr esi
lea esi, myFunc
call esi
call label1 ptr esi
call label2 ptr esi

push 0
call ExitProcess
main endp

end

Output:

Hello from called function
Hello from called function
Hello from called function
Hello from called function
Hello from called function
Hello from called function

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 6208
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Question about TYPEDEF in masm
« Reply #7 on: April 14, 2019, 05:13:45 PM »
 :biggrin:

Be careful of what you wish for, you can provide sheer confusion and get nothing out of it.

TYPEDEF MADNESS

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

    include \masm32\include64\masm64rt.inc

    WOTTA_LOTTA_ROT typedef PTR
    INT_UNKNOWN_SIZE_AND_NAME_BOVINE_EXCREMENT typedef PTR WOTTA_LOTTA_ROT
    YETTA_NUTHA_MINDLESS_TYPEDEF_NAME typedef PTR INT_UNKNOWN_SIZE_AND_NAME_BOVINE_EXCREMENT
    WTF_IS_THIS_PILE_OF_CRAP typedef YETTA_NUTHA_MINDLESS_TYPEDEF_NAME
    UNKNOWABLE_TYPE typedef PTR WTF_IS_THIS_PILE_OF_CRAP

    .code

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

entry_point proc

  ; bsvar# = bullsh*t variable

    LOCAL bsvar1 :UNKNOWABLE_TYPE

    % echo +++++++++++++++++++++++++++++++++
    % echo UNKNOWABLE_TYPE = varsize(UNKNOWABLE_TYPE) bytes
    % echo +++++++++++++++++++++++++++++++++

    mov rax, 12345678
    mov bsvar1, rax
    conout str$(rax),lf

    waitkey

    invoke ExitProcess,0

    ret

entry_point endp

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

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

AW

  • Member
  • *****
  • Posts: 1873
  • Let's Make ASM Great Again!
Re: Question about TYPEDEF in masm
« Reply #8 on: April 14, 2019, 05:24:21 PM »
 :badgrin:  :t

StillLearningMasm

  • Regular Member
  • *
  • Posts: 6
Re: Question about TYPEDEF in masm
« Reply #9 on: April 16, 2019, 04:58:50 AM »
Thank you AW. That was exactly what I was looking for.

Do these instruction create the same code?

Label1 typedef
Label1 typedef ptr

According to the listing file if I create code and name them as this:

Label1 typedef
Label2 typedef ptr

Then the listing file shows the attr for Label2 as ptr but shows blank for Label1.
Are they two different types? Is Label2 a pointer type? I think it is.
But I have no idea what type is Label1.

Still trying to understand TYPEDEF.

I love the example from hutch--.  :lol:

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 6208
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Question about TYPEDEF in masm
« Reply #10 on: April 16, 2019, 07:30:45 AM »
Have a look at a couple of basic things, there is no difference in 32 bit between a normal DWORD variable and a pointer. In 32 bit ALL pointers are the native data size, 4 bytes in length. The only factor that a processor understands is the data SIZE, not its name. While TYPEDEF statements are usable for renaming data sizes and their use, it comes at the price that you no longer know how BIG the data is and this is an endless source of data size errors.

If you start off the bottom, size based data you are free of the confusion and with practice you will get to remember things like BYTE PTR which is the ADDRESS of a BYTE in memory. A BYTE PTR is standard Intel notation in x86 and while you can TYPEDEF it to things like PCHAR and the like, you gain nothing from doing it and you lose the reference to how BIG the data is.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin:

StillLearningMasm

  • Regular Member
  • *
  • Posts: 6
Re: Question about TYPEDEF in masm
« Reply #11 on: April 16, 2019, 03:47:06 PM »
Thank you for the explanation Hutch--.

So for the examples I was originally talking about:

Label1 typedef
Label2 typedef ptr

Label1 is not a pointer and has no defined size. Shouldn't this be an error when creating it? Masm gives no error unless I attempt to make it local in a proc:

local pmystr1:Label1
c:\masm32\bin\assembly2.asm(152) : error A2195: parameter or local cannot have void type.

Label2 is a pointer it does not need a defined size.

Am I understanding this correctly?




hutch--

  • Administrator
  • Member
  • ******
  • Posts: 6208
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Question about TYPEDEF in masm
« Reply #12 on: April 16, 2019, 05:50:44 PM »
Pretty close to it.

error A2195: parameter or local cannot have void type.

This is the same as in a high level language,

variable =

Its an error if you don't have something to assign to the variable.

You have data types and depending on the OS version, in 32 bit, a PTR is 32 bit, in Win64 a pointer = QWORD (8bytes).

Know the OS version and you know the native data size for a pointer. You have the rest of the integer data sizes,

BYTE
WORD
DWORD
QWORD

There are larger 128 and 256 data types but you won't need them yet.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin:

AW

  • Member
  • *****
  • Posts: 1873
  • Let's Make ASM Great Again!
Re: Question about TYPEDEF in masm
« Reply #13 on: April 17, 2019, 02:00:36 AM »
Destroying Masm small brain, aka Buffer Overflow  :bgrin:

Code: [Select]
.486

.model flat, stdcall

ExitProcess proto :dword

label1 typedef ptr CRAP

.data
msg  db 'Blowing MASM',10,0
crap label1 1000000

.code

main proc
invoke Exitprocess, 0
main endp

end

Microsoft (R) Macro Assembler Version 14.20.27508.1
Copyright (C) Microsoft Corporation.  All rights reserved.

 Assembling: testB.asm

MASM : fatal error A1016: Internal error

  Version 14.20.27508.1

  ExceptionCode            = C0000005
  ExceptionFlags           = 00000000
  ExceptionAddress         = 0089F3E7 (00870000) "H:\masm32\bin\ml.exe"
  NumberParameters         = 00000002
  ExceptionInformation[ 0] = 00000000
  ExceptionInformation[ 1] = 00008408

CONTEXT:
  Eax    = 00000000  Esp    = 010FF340
  Ebx    = 00000001  Ebp    = 010FF344
  Ecx    = 00008400  Esi    = 00000000
  Edx    = 00000000  Edi    = 00008400
  Eip    = 0089F3E7  EFlags = 00010202
  SegCs  = 00000023  SegDs  = 0000002B
  SegSs  = 0000002B  SegEs  = 0000002B
  SegFs  = 00000053  SegGs  = 0000002B
  Dr0    = 00000000  Dr3    = 00000000
  Dr1    = 00000000  Dr6    = 00000000
  Dr2    = 00000000  Dr7    = 00000000

HSE

  • Member
  • ****
  • Posts: 980
  • <AMD>< 7-32>
Re: Question about TYPEDEF in masm
« Reply #14 on: April 17, 2019, 02:20:24 AM »
Destroying Masm small brain, aka Buffer Overflow  :bgrin:

Just a bug in ML14

Code: [Select]
X:\masm32\test>ml crap.asm
Microsoft (R) Macro Assembler Version 6.14.8444
Copyright (C) Microsoft Corp 1981-1997.  All rights reserved.

 Assembling: crap.asm
crap.asm(11) : error A2004: symbol type conflict : CRAP
crap.asm(11) : fatal error A1016: Internal Assembler Error