News:

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

MASM High Level Questions

Started by n00b!, June 10, 2008, 10:28:15 AM

Previous topic - Next topic

n00b!

1. What means "abc equ $"
As i know, equ is somehow like the #define Preprocessor-directive in C, but what is, if a '$' is behind it?

2. My second question:

if in C:
Code:
if(a == b) {
printf("%c", c);
}

in asm:
Code:
cmp ebx, eax
jne abc
call something
abc:
...

For an "if-flow control/control structure" i need a label with a name (e.g. abc:)
In C there is no need for this, because you specify a command-block with { }
In MASM, too... with these Pseudo-High Level- F.C./C.S. .IF, .WHILE, etc.

in masm:
Code:
.IF a == b
call something
.ENDIF

So I don't need a label/labelname..
But how does MASM do this?
Does it generate a random label-name or something like that?
What is, if I accidentally use the same label name?

3.:
How would look this MASM code in ASM?
Code:
.IF a == 2 && b == 3 || c == 1
invoke MessageBox, 0, 0, 0, 0
.ENDIF

And can i use parentheses like this?:
Code:
.IF a == 2 && (b == 3 || c == 1)
invoke MessageBox, 0, 0, 0, 0
.ENDIF

And another question:

How do @@-Labels work?
And for what is jmp @B and jmp @F?

Thanks in Advance!

hutch--

Hi nob,,

i will try on some of your qestions.

The "$" by itself is called the "current location pointer" and it literally is the current instruction pointer or IP. It means the OFFSET in memory of where the current location is.

An equate to it means you can use the "abc" in the same way.

The "something" you call when you call a function by its name is actualy an OFFSET in memory, it behaves exactly like a label. I fact you can write very low level function that start with a label and exit on the RET at the other end.

The @@: labels are called anonymous laels and they save you from having to invent new names for at least some labels where the context is understoof from the code. The jmp @F means jump to the NEXT @@: label, the jmp @B means jump BACK to the last @@: label.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

gxm

hi,nob
all of the three questions could be solved by a piece of sample code...

.386
.model flat,stdcall
option casemap:none

include windows.inc
include user32.inc
include kernel32.inc
includelib kernel32.lib
includelib user32.lib
.const
db 'just for test'
abc equ $
a db 1
b db 2
d db 3   ;pay attention:not c
.code
start:
mov eax,abc
invoke MessageBox,0,0,0,0
.if a==1&&(b==2||d==3)
invoke MessageBox,0,0,0,0
.endif
.if a==1&&b==2||d==3
invoke MessageBox,0,0,0,0
.endif
invoke ExitProcess,0
end start

after compile and link,u can use od and ida pro to watch the result.
1.u'll find that abc will be 0x40201d just the address after the string "just for test".[in ollydbg]
2.u'll find that MessageBox is address of another instruction like jmp imp_MessageBox,which is exactly the code which calls the API function.[in IDA pro]
3.u'll find that the assembly code as follows:

               public start
.text:00401000 start           proc near
.text:00401000                 mov     eax, offset byte_40201D
.text:00401005                 push    0               ; uType
.text:00401007                 push    0               ; lpCaption
.text:00401009                 push    0               ; lpText
.text:0040100B                 push    0               ; hWnd
.text:0040100D                 call    MessageBoxA
.text:00401012                 cmp     ds:byte_40201D, 1   ;here 40201d mean a
.text:00401019                 jnz     short loc_40103A
.text:0040101B                 cmp     ds:byte_40201E, 2    ;d<---->we cannot ues c as variable in Masm32v9
.text:00401022                 jz      short loc_40102D
.text:00401024                 cmp     ds:byte_40201F, 3    ;b
.text:0040102B                 jnz     short loc_40103A
.text:0040102D
.text:0040102D loc_40102D:                             ; CODE XREF: start+22j
.text:0040102D                 push    0               ; uType
.text:0040102F                 push    0               ; lpCaption
.text:00401031                 push    0               ; lpText
.text:00401033                 push    0               ; hWnd
.text:00401035                 call    MessageBoxA
.text:0040103A
.text:0040103A loc_40103A:                             ; CODE XREF: start+19j
.text:0040103A                                         ; start+2Bj
.text:0040103A                 cmp     ds:byte_40201D, 1
.text:00401041                 jnz     short loc_40104C
.text:00401043                 cmp     ds:byte_40201E, 2
.text:0040104A                 jz      short loc_401055
.text:0040104C
.text:0040104C loc_40104C:                             ; CODE XREF: start+41j
.text:0040104C                 cmp     ds:byte_40201F, 3
.text:00401053                 jnz     short loc_401062
.text:00401055
.text:00401055 loc_401055:                             ; CODE XREF: start+4Aj
.text:00401055                 push    0               ; uType
.text:00401057                 push    0               ; lpCaption
.text:00401059                 push    0               ; lpText
.text:0040105B                 push    0               ; hWnd
.text:0040105D                 call    MessageBoxA

======
so just use the tool to dig out what u r wondering.

Jimg

Also, you can get a listing from Masm by adding the  /Fl parameter.
Just remember to put .nolist, .list around the library includes-

.nolist
include windows.inc
include user32.inc
include kernel32.inc
includelib kernel32.lib
includelib user32.lib
.list

or your listing will be huge.

n00b!

Thanks!!

I'll read it now :-)

Edit:
Does db save bytes at the current position?

And if you specify a name, you can use the name as a pointer for the address (so it is like a variable)?

tt db "a", "b", 13, 10     ; pos(a) = 0x100, pos(b) = 0x101, pos(\n) = 0x102, pos(\r) = 0x103
db "c", "defg", 0          ; pos(c) = 0x104, pos(defg) = 0x105 - 0x108, pos(\0) = 0x109


If I use tt, the whole bytes will be read until a \0 occurs, isn't it so?
So I can also access the "cdefg", because they are directly behind "ab\n\r" and because there was no \0?


Edit2:
So if I write:

start equ $
text db "aaaaaaaaaaaaaaaaaaa",0
a dd 10
b dw 5
d db "c"
end equ $ - start


The equ/constant end stands for the size of bytes which where reserved by the variables?

dd stands for DWORD, dw WORD and db BYTE, but does it make a difference if I write DWORD or BYTE, like?:

.data
a dw 10
k BYTE 1
b dd 20

.data?
z WORD ?
u dd ?
l DWORD ?
k db ?

hutch--

noob,

With BYTE data in the iniialised data section, it is written sequentially so you can write code like this.


.data
item db "line 1",13,10
     db "line 2",13,10
     db "line 3",13,10
     db "last line",13,10,0  ;< note the only zero terminator
.code
  invoke StdOut,OFFSET item
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

BogdanOntanu

Quote
dd stands for DWORD, dw WORD and db BYTE, but does it make a difference if I write DWORD or BYTE, like?:

db, dw,  dd etc are used to "define data"
BYTE, WORD, DWORD are considered more like types of data

Hence by convention it is more stylish to used db,dw,dd and friends to define data and variables and it is considered more stylish to used BYTE WORD, DWORD and friends inside STRUCture definition to define the sizes and types of data members. However you could used DB,DW,DD in structures.

You should used BYTE, WORD, DWORD when declaring LOCAL variables inside a PROC because there you do no "define" any data just the size/type of it.


The intermix of DB/DW/DD and BYTE/WORD/DWORD is confusing for beginner so use the above rule of thumb:
1) when you declare or define data variables use DB/DW/DD
2) in STRUC use what you prefer
3) in PROC LOCAL variables use BYTE,WORD,DWORD.

Note that STRUCTURES are in somehow the middle being both data types and data define keywords at times.
Ambition is a lame excuse for the ones not brave enough to be lazy.
http://www.oby.ro

Jimg

Bogdan-

  Can one use dd,dw,and db in locals in SolAsm?   Masm absolutely chokes on this

xxx proc
local aq2:dd,aq3:db

gxm

to n00b:
whether you can acess the "cdefg" or not  depend on which insturction/API you use.
   for example:
      i>if you use:cmp al,ttit will never acess "cdefg" even "b"
      ii>if you use:invoke MessageBox,offset tt,0,0,0surely the API will acess tt till encout the '\0'.

n00b!

Ok, I think I got the most, thanks  :U

But something's left...

1.
db, dw, dd, ... save bytes at a specific (current?) memory position.
If you specify a name in front of the instruction, the name is like a pointer to the memory address of the specified bytes (behaves like a variable)..
Is that correct?

2.
start equ $
text db "aaaaaaaaaaaaaaaaaaa",0
a dd 10
b dw 5
d db "c"
end equ $ - start


The equ/constant "end" contains the size of bytes which where reserved by the variables?

3.
What do I have to write in the const section?
Variables, Includes, equs, ...?

4.
When I write sources with brackets, I'll get the memory position, like:
mov al, [ebx]
Will the source have the size of 1 BYTE because of the brackets?

So that I can't write:
mov ebx, [ax]

        ;ebx = 4 Bytes (32bit), ax = 2 Bytes (16bit), [ax] = 1 Byte (8bit) because of brackets


But I have to write:
mov ebx, DWORD ptr [ax]

        ;[ax] = 1 Byte, but DWORD ptr = get 4 Bytes from [..] as memory position?


Is this correct? :-/

gxm

to n00b!:
1.yes,you can take the label as a pointer,but not a variable anyway.To be exactly,the label is just the address of the data or code.
--
2.yes. it is.
--
3.in const section.you should define constant data.ie.strings as the caption of one MessageBox;
   equ could be define anyway of the file header(means before the .code section);
   include should be placed just after the code:
   
   .386 
   .model flat,stdcall
   option casemap:none
 

   anyway,thx to jj2007,we get to know that just use
     include /masm32/include/masm32crt.inc to replace all the include like directives.
---
4.to get the address of one variable.
  we can use:
 
  xxx db 123
  mov eax,offset xxx
or   lea eax,xxx
  and we can get the value of the variable by following code:
    mov eax,xxx
  or
 
  lea eax,offset xxx
  mov ebx,[eax]
 

 
  and we cannot get the address of one register like eax.
  we can just get the address of data in the memory....
  ps:
  u should read some introduction book like <the art of assembly language> to gain the basis of assembly language.[and i guess you 're a high language victim.]..he..he
-------
  regs,
  gxm.
 

jj2007

Quote from: gxm on June 11, 2008, 02:18:46 PM
  include /masm32/include/masm32crt.inc
No "c"... correct version:

include \masm32\include\masm32rt.inc

This file starts with .486 etc., and includes everything you need in practice, e.g. the Masm32 macros (a must! e.g. m2m ="mem to mem").

      .486                                      ; create 32 bit code
      .model flat, stdcall                      ; 32 bit memory model
      option casemap :none                      ; case sensitive

;     include files
... lots of ...
      include \masm32\macros\macros.asm         ; masm32 macro file

n00b!

Sorry, I'm not good in expressing something in English ._.

I meant with the 4th point something like this:

.386
.model flat, stdcall
option casemap :none

.data
  text db "abcde", 0

.code
start:
  mov eax, DWORD ptr [text + 1]   ;eax = bcde  eax & DWORD = 4 bytes
  mov ax, WORD ptr [text + 1]     ;eax = bc     ax &  WORD = 2 bytes
  mov al, BYTE ptr [text + 1]     ;eax = b      al &  BYTE = 1 byte
 
  mov al, [text + 1]              ;eax = b      al &  BYTE = 1 byte
 
  mov eax, WORD ptr [text + 1]    ;does'nt work: eax = 4 bytes,  WORD = 2 bytes
  mov al, DWORD ptr [text + 1]    ;does'nt work:  al = 1 byte , DWORD = 4 bytes
end start


The Instruction "mov al, [text + 1]" works without specifieng the size of the bytes ("XXX ptr").
I gather from this that the size of the bytes, which are got from a value in brackets, ever is 1 byte.
So the following should work:
mov BYTE ptr eax, [text + 1]
mov al, [text + 1]


Is this so, that when I use the brackets I don't have to specify another size, when I need (1) byte, because it is ever 1 byte with brackets?

When this is true, why does this not work?
cmp [eax + ecx], 22h              ;"BYTE ptr [eax + ecx]" would work

"[eax + ecx]" = 1 Byte

" 22h" = 1 Byte?

PS: Sorry for my bad English

jj2007

Basically, Masm wants to know the size of operands. If there is ambiguity, it will choke.

.data
vdw   dd 0
vw   dw 0
vb   db 0

.code
mov eax, vdw
mov ax, vw
mov al, vb
mov ah, vb

cmp eax, 12345
cmp ax, 1234
cmp al, 123
cmp [vdw+eax], 123
cmp [vdw+ax], 123  ; ok...!
; cmp [vdw+al], 123  ; no
; cmp [vdw+ax], ax ; no
; cmp [vdw+ax], al ; no

n00b!

I don't get it, sorry :eek

ps: Does my last example maybe have something to do with immerdiate values (I don't know how they're called, but I mean some kind of direct values...)