The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: bf2 on June 01, 2011, 05:05:45 PM

Title: Procedure calls
Post by: bf2 on June 01, 2011, 05:05:45 PM
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.
Title: Re: Procedure calls
Post by: jj2007 on June 01, 2011, 05:08:04 PM
.DATA
MyVar  dd 123

.CODE
MyProc PROC
RET
MyProc ENDP

start:
Title: Re: Procedure calls
Post by: cobold on June 01, 2011, 05:08:49 PM
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

Title: Re: Procedure calls
Post by: dedndave on June 01, 2011, 06:19:39 PM
if you are using masm32rt.inc, you do not need .386 or OPTION CASEMAP:NONE, as that file has them
Title: Re: Procedure calls
Post by: mineiro on June 01, 2011, 06:55:36 PM

.386
.model flat, stdcall
option casemap:none

.code
start:
ret
end start
Title: Re: Procedure calls
Post by: bf2 on June 01, 2011, 08:17:52 PM
Great stuff, thanks a lot.
Title: Re: Procedure calls
Post by: JayJay on June 03, 2011, 11:17:56 AM
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.
Title: Re: Procedure calls
Post by: dedndave on June 03, 2011, 11:31:41 AM
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
Title: Re: Procedure calls
Post by: dedndave on June 03, 2011, 11:36:37 AM
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
Title: Re: Procedure calls
Post by: JayJay on June 03, 2011, 11:54:05 AM
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++?
Title: Re: Procedure calls
Post by: dedndave on June 03, 2011, 11:58:48 AM
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
Title: Re: Procedure calls
Post by: JayJay on June 03, 2011, 12:07:59 PM
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
Title: Re: Procedure calls
Post by: dedndave on June 03, 2011, 12:48:23 PM
yes - and let's not forget "class", which makes no sense to me - lol
Title: Re: Procedure calls
Post by: MichaelW on June 03, 2011, 03:58:30 PM
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

Title: Re: Procedure calls
Post by: JayJay on June 03, 2011, 05:43:38 PM
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.


Title: Re: Procedure calls
Post by: cobold on June 03, 2011, 06:27:39 PM
Hi,

QuoteWhat i try to do is initialize a variable called testInt.
...
...

testInt=7

to initialize an unsigned/signed integer:

testInt DWORD 7 ; unsigned
testInt dd 7 ; same as above but less typing :-)
testInt SDWORD 7 ; signed


Quote
When i put as argument value '-10' i get a strange value.
....
But how do i store a negative value in a register?

You simply "mov" the value to the register:
mov eax,-10

If you know your value is signed, use str$(eax), if unsigned --> ustr$(eax), OR

invoke dwtoa,eax,ADDR someText to convert SIGNED value to Ascii.

hth



Title: Re: Procedure calls
Post by: JayJay on June 03, 2011, 06:36:12 PM
Hi cobold,

I succeeded thank you verry much. :U The problem was just super small. I needed to change this line
    invoke  MessageBox,0, ustr$(eax),ADDR capt,MB_OK

into:
    invoke  MessageBox,0, str$(eax),ADDR capt,MB_OK