0

I want pass "msg" and "len" to a external procedure, i don't know why. The compiler said undefined variables.

File1.asm:

%include "File2.asm"
extern write_string
section .data
   msg db "Hello world",0xA
   len equ $- msg
section .text
    global _start 
_start:
    call write_string

    mov rax,1           ;system call number (sys_exit)
    int 0x80            ;call kernel

File2.asm

write_string:
         mov    rax,4           ;system call number (sys_write)
         mov    rbx,1           ;file descriptor (stdout)
         mov    rcx, msg    
         mov    rdx, len
         int    0x80            ;call kernel
         ret
LordPaella
  • 143
  • 9
  • 2
    Normally you pass args in registers, according to the normal calling convention. What you're trying to do is access static data from another file. (And an assemble-time constant, which I don't think you can do.) And BTW, `int 0x80` in 64-bit code is a bad idea, it will break if you link this as a PIE executable. [What happens if you use the 32-bit int 0x80 Linux ABI in 64-bit code?](https://stackoverflow.com/q/46087730) – Peter Cordes Dec 13 '18 at 07:31
  • I can pass variables using %macro, but i don't want use it, because in some documentation i found is better using procedures. – LordPaella Dec 13 '18 at 07:40
  • The way you are doing it isn't good coding practice regardless. As Peter says, it's better to pass them as arguments and that can be done in registers. For example, move `len` and `msg` into `rdx` and `rcx` before calling `write_string`, then they will already be there when `write_string` is called (`write_string` does not need to move them into those registers). – lurker Dec 13 '18 at 11:24
  • Ok i use 'syscall', why can i pass variables in a %macro?, what is the diference with a procedure? – LordPaella Dec 13 '18 at 11:46
  • 1
    A macro expands into code directly inline in your current module. So any references are in scope of what you have in that module. If you define a function in a separate file, it doesn't "see" the variables you've defined in the first file. If you were to take the contents of your `file2.asm` and put it into `file2.asm` it would work. – lurker Dec 13 '18 at 17:36

0 Answers0