NASM - The Netwide Assembler
NASM Forum => Programming with NASM => Topic started by: dindibo4 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
; 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?
-
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:
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:
%define aParam [ebp+4]
%define bParam [ebp+8]
-
I personally prefer the simpler method using by using %define because of simplicity, but thanks for the answer.
-
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.