user2827214
user2827214

Reputation: 1191

Simple macro parameter syntax in x86 Assembly, address calculation

Not sure why I cannot use eax in the address calculation... the following return an invalid operand type error...

I have a macro that takes one parameter:

    %macro  IdPrompt 1      ; 1% - the offset within Buff at which to write the ID that is read in                                                                                                                             

    mov eax, 3              ; Specify sys_read call                                                                                                                                                                            
    mov ebx, 0              ; Specify File Descriptor 0: Stdin                                                                                                                                                                 
    mov ecx, Buff+([%1]*2)  ; Pass offset of the buffer to read ID into, 2 is the scale                                                                                                                                                        
    mov edx, 3              ; Pass number of bytes to read at one pass                                                                                                                                                         
    int 80h                 ; call sys_read to fill the buffer                                                                                                                                                                 
    %endmacro

The macro is called by this within another macro:

    IdPrompt eax            ; call IdPrompt to get one ID

I tried using a smaller register, as well as with the Buffer address as: Buff+%1*2 with no luck

Upvotes: 0

Views: 958

Answers (1)

Johan
Johan

Reputation: 76537

As per @user786653's comment:

The correct syntax is:

%macro  IdPrompt 1      ; 1% - the offset within Buff at which to write the ID that is read in                                                                                                                             

mov eax, 3              ; Specify sys_read call                                                                                                                                                                            
mov ebx, 0              ; Specify File Descriptor 0: Stdin                                                                                                                                                                 
lea ecx, [Buff+%1*2]    ; Pass offset of the buffer to read ID into, 2 is the scale                                                                                                                                                        
mov edx, 3              ; Pass number of bytes to read at one pass                                                                                                                                                         
int 80h                 ; call sys_read to fill the buffer                                                                                                                                                                 
%endmacro

Note that lea never accesses memory; it only does address calculation.
So even though the argument is in [] square brackets, you're using the literal register values.

The %1 gets replaced by a register reference. In your example: lea ecx,[eax*2+Buff]

Upvotes: 1

Related Questions