News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Procedure calls

Started by bf2, June 01, 2011, 05:05:45 PM

Previous topic - Next topic

bf2

Hi guys,

I am trying to assemble a simple procedure.

If I try to assemble the following:

.386
OPTION CASEMAP:NONE
include \masm32\include\masm32rt.inc

MyProc PROC
RET
MyProc ENDP

.DATA
.CODE
start:
CALL MyProc

INVOKE ExitProcess, NULL
END start


then I am getting three assembly-time errors:

Test.asm(5) : error A2034: must be in segment block : MyProc
Test.asm(6) : error A2034: must be in segment block
Test.asm(7) : fatal error A1010: unmatched block nesting : MyProc


If I try using prototypes,

.386
OPTION CASEMAP:NONE
include \masm32\include\masm32rt.inc

MyProc PROTO

.DATA
.CODE
start:
CALL MyProc

INVOKE ExitProcess, NULL
END start

MyProc PROC
RET
MyProc ENDP


then it assembles without error but then I get the following linking error:
error LNK2001: unresolved external symbol _MyProc@0
fatal error LNK1120: 1 unresolved externals


Help, please.

jj2007

.DATA
MyVar  dd 123

.CODE
MyProc PROC
RET
MyProc ENDP

start:

cobold

Hi,

just move your MyProc into the .CODE section:
.386
OPTION CASEMAP:NONE
include \masm32\include\masm32rt.inc

MyProc PROTO

.DATA
.CODE
start:
CALL MyProc

INVOKE ExitProcess, NULL

;-------------------
MyProc PROC
RET
MyProc ENDP

END start


dedndave

if you are using masm32rt.inc, you do not need .386 or OPTION CASEMAP:NONE, as that file has them

mineiro


.386
.model flat, stdcall
option casemap:none

.code
start:
ret
end start

bf2

Great stuff, thanks a lot.

JayJay

Hi,

I'am new to this forum. I want to learn assembly. I got almost 1,5 years of experience with C++ with creating video games + 1 year of Java programming. In the last few days i searched a lot on this forum. Also i tryed to create some super tinny programs. Like adding a nummer or substracting a number.

I replyed on this thread because i got a few questions about procedures (PROC). Is a procedure something similair like a method in java or a function in C++?

Here is my second question;

Below you see my code. What i try to do is initialize a variable called testInt. Via a procedure i want to add a value to it. This code assembles oké. I get no errors. But when i click on the .exe file my program crashes.


.386
.model flat,stdcall
option casemap:none

include     \masm32\include\windows.inc
include     \masm32\include\kernel32.inc
include     \masm32\include\user32.inc

includelib  \masm32\lib\kernel32.lib
includelib  \masm32\lib\user32.lib

MyProc proto

.data

format1     db 'The value of eax = %d',0
capt        db 'Hello',0
testInt=7

.data?

buffer      db 100 dup(?)

.code

start:


    invoke MyProc

    invoke  wsprintf,ADDR buffer,ADDR format1,eax
    invoke  MessageBox,0,ADDR buffer,ADDR capt,MB_OK
    invoke  ExitProcess,0


MyProc proc
    add eax, 10
MyProc endp


END start


Could somebody explain what i'am doing wrong?

JayJay.

dedndave

welcome to the forum, JayJay   :U

first, i am a little surprised that it assembles   :P
wsprintf should be an unresolved external, as kernel32 and user32 do not have it, as far as i know
you might want to add
include \masm32\include\msvcrt.inc
includelib \masm32\lib\msvcrt.lib

or, you can use masm32rt.inc, which takes care of most preamble stuff, including processor and model

second, your proc has no ret
MyProc proc
    add eax, 10
    ret
MyProc endp

dedndave

try this
        INCLUDE \masm32\include\masm32rt.inc

MyProc proto

.data

capt        db 'Hello',0

.code

start:

    mov    eax,0
    invoke MyProc

    invoke  MessageBox,0,ustr$(eax),ADDR capt,MB_OK
    invoke  ExitProcess,0


MyProc proc
    add eax, 10
    ret
MyProc endp


END start

JayJay

Thank you verry much for your reply. Hahah you'r code works super smooth. :clap:

About your code i got a silly question. When i read about masm you need to specify wich instruction set you will use, .386, .486(p) Why assembles you code correctly without it? And also you code has no .MODEL stdcall.

Is a procedure something similair as a function in c++?

dedndave

i used the masm32rt.inc file
have a look inside that file - it takes care of processor, model, casemap, includes, and libraries
sometimes, you may need to modify the values after the masm32rt include directive
for example, some instructions require a higher processor or additional directives
another example is additional includes and/or libraries, like advapi32

from my understanding of C - yes - a function is similar to a procedure
we sometimes refer to them as routines   :P

JayJay

Aha oki thank you verry much for your explanation. I'll take a look at the file.

I'am a student. In the last 1,5 years i have learned several names for the same thing. Function, prodecure, method(accessor method, mutator method) and a new one 'routines'.  :bdg

dedndave

yes - and let's not forget "class", which makes no sense to me - lol

MichaelW

Dave,

wsprintf does look like a CRT function, but it's actually from User32.dll, and it does not support the floating-point type specification:

http://msdn.microsoft.com/en-us/library/ms647550(VS.85).aspx

eschew obfuscation

JayJay

Hi,

I'am expirementing with writinging proc. I've written a few proc without arguments. Also i have written one proc with one argument. A DWORD. So a DWORD is a 32 bit unsigned int. So i can only put positive numbers in the variable.

So i tryed to create a new proc so that i could give  signed int als argument. So i took the reference of msdn. It says that i need a SDWORD, a 32 signed integer.

proto.:
addArg proto blaat:SDWORD

and the function.:
addArg proc blaat:SDWORD
  add eax, blaat
  ret
addArg endp


invoke.:
    invoke addArg, -10

When i put instead of '-10', 10 as argument, the output is ok (i write the value of eax to a message box).

When i put as argument value '-10' i get a strange value. 4294967286. Hmm ok it isn't that strange it is the maximum 32 unsigned int value.

On the internet i read something that every register holds a DWORD. So every register only fits a 32 unsigned int. But how do i store a negative value in a register?

JayJay.