Author Topic: bad syntax for EQU - when using aParam equ [rbp+16]  (Read 102 times)

Offline dindibo4

  • Jr. Member
  • *
  • Posts: 2
bad syntax for EQU - when using aParam equ [rbp+16]
« on: July 04, 2019, 02:25:12 PM »
Hello, i'm pretty new to NASM and I've tried to make a procedure like printf and assign the arguments with EQU

Code: [Select]
; void printf(char *text, int len)
printf:
push rbp
mov rbp, rsp

aParam equ [rbp+16]
bParam equ [rbp+24]

mov eax, 4
mov ebx, 1
mov ecx, bParam
mov edx, aParam
int 0x80

pop rbp
ret
 

It didn't worked and showed "bad syntax for EQU" exception on the code.
What is the right syntax of EQU for this usage?

Offline fredericopissarra

  • Jr. Member
  • *
  • Posts: 47
Re: bad syntax for EQU - when using aParam equ [rbp+16]
« Reply #1 on: July 04, 2019, 03:56:52 PM »
First. Since you are using the stack to pass the arguments for your function AND 'int 0x80', I assume you are dealing with 32 bits SysV ABI. You cannot use Rxx registers there.

Second, there is a better approach:
Code: [Select]
bits 32   ; to be sure!

section .text

global putstr

; Stack frame for putstr() function call.
struc putstrstk
.retaddr: resd  1   ; call will push the return address.
.len:     resd  1   ; cdecl calling convention will push len first,...
.textptr: resd  1   ; ... then, textptr, before calling putstr().
endstruc

; void putstr(char *text, size_t len)
putstr:
  ; You don't need to use EBP!!!
  mov eax, 4      ; write syscall [write( int fd, void *buff, size_t size );]
  mov ebx, 1      ; stdout
  mov ecx, [esp + putstrstk.textptr]
  mov edx, [esp + putstrstk.len]
  int 0x80
  ret

Third, EQU is not a lexical substitution directive. For that purpose you should use %define:
Code: [Select]
%define aParam [ebp+4]
%define bParam [ebp+8]
« Last Edit: July 04, 2019, 04:10:40 PM by fredericopissarra »

Offline dindibo4

  • Jr. Member
  • *
  • Posts: 2
Re: bad syntax for EQU - when using aParam equ [rbp+16]
« Reply #2 on: July 04, 2019, 07:25:30 PM »
I personally prefer the simpler method using by using %define because of simplicity, but thanks for the answer.

Offline fredericopissarra

  • Jr. Member
  • *
  • Posts: 47
Re: bad syntax for EQU - when using aParam equ [rbp+16]
« Reply #3 on: July 04, 2019, 08:32:27 PM »
I personally prefer the simpler method using by using %define because of simplicity, but thanks for the answer.
Ok... just keep in mind that using the hardcoded [ebp+offset] can lead to bugs. In your example the offsets are wrong... Using the structure you avoid this problem.