The MASM Forum

General => The Campus => Topic started by: xponential on March 08, 2014, 02:53:47 AM

Title: Help a beginner [addressing modes]. . . .
Post by: xponential on March 08, 2014, 02:53:47 AM
Good day Everyone! I'm not sure what i'm doing wrong but here is the little piece of code thats giving me problem:
.386
.model flat, stdcall
option casemap: none
include windows.inc
include kernel32.inc
include masm32.inc
includelib kernel32.lib
includelib masm32.lib
.data
Data_Items dd 12, 15
.data?
Item dd ?
.code
start:
mov edx, Data_Items
mov ecx, 4
mov eax, [edx+ecx]
push eax
pop character
invoke StdOut, addr character
invoke ExitProcess, 0
end start

anytime i assembly this, the program opens and crashes immediately, my aim is to output '15'. Please whats wrong with the code? I even used:
mov character, eax
instead of push, pop but i get same error, thanks.
Title: Re: Help a beginner [addressing modes]. . . .
Post by: qWord on March 08, 2014, 03:18:46 AM
For MASM data labels represent the variable and not the address. To get the address use the OFFSET operator:
mov edx,OFFSET Data_Items
Remarks that you can also directly use the label as memory operand:
mov ecx, 4
mov eax, Data_Items[ecx]

or
mov eax, Data_Items[4]

Title: Re: Help a beginner [addressing modes]. . . .
Post by: xponential on March 08, 2014, 03:41:21 AM
Thanks buddy, it now works :)
Title: Re: Help a beginner [addressing modes]. . . .
Post by: jj2007 on March 08, 2014, 03:44:54 AM
Quote from: xponential on March 08, 2014, 03:41:21 AM
Thanks buddy, it now works :)

Surprising - there were some other issues ;-)

Here is one more form of indirect addressing:

mov edx, offset Data_Items

mov ecx, 4
mov eax, [edx+ecx]  ; = [edx+4], see qWord's post

mov ecx, 1
mov eax, [edx+4*ecx]  ; = [edx+4*1]
Title: Re: Help a beginner [addressing modes]. . . .
Post by: xponential on March 08, 2014, 05:03:14 AM
^ Thanks for the additional info, much appreciated. On the other hand, how do i output numbers as their decimal value rather than ASCII?
Title: Re: Help a beginner [addressing modes]. . . .
Post by: jj2007 on March 08, 2014, 05:36:31 AM
include \masm32\include\masm32rt.inc

.code
start:
   mov eax, 12345
   MsgBox 0, str$(eax), "A number:", MB_OK
   exit

end start


Have a look at the \Masm32\Help folder...
Title: Re: Help a beginner [addressing modes]. . . .
Post by: Gunther on March 08, 2014, 05:48:46 AM
Hi xponential,

and welcome to the forum.

Gunther
Title: Re: Help a beginner [addressing modes]. . . .
Post by: dedndave on March 08, 2014, 10:56:12 PM
the str$ macro will do it for you, as Jochen mentioned
if you are curious, you can always look at the macros, as well as the functions they call
the \masm32\help\hlhelp.chm file will give you some info on using the macros

another thing worth mentioning...
Quoteanytime i assembly this, the program opens and crashes immediately

we often place an "inkey" (another macro) before the ExitProcess
that way, you can see the results before the console window closes
Title: Re: Help a beginner [addressing modes]. . . .
Post by: xponential on March 09, 2014, 11:09:01 AM
thanks dendev, i'll look into that :)