汇编程序中的语法格式

时间:2015-03-26 15:48:21

标签: assembly nasm x86-64

是否有可能以某种方式简化此代码?这是我的许多功能的一般模式。我不想每次都做global行。

global write
write:      mov rax, 1
            syscall
            ret

global open
open:       mov rax, 2
            syscall
            ret

global close
close:      mov rax, 3
            syscall
            ret

修改

nasm,64位

2 个答案:

答案 0 :(得分:2)

您可以创建一个宏,例如:

%macro public 1
global %1
%1
%endmacro

public write:
            mov rax, 1
            syscall
            ret

public open:
            mov rax, 2
            syscall
            ret

public close:
            mov rax, 3
            syscall
            ret

您可以将宏定义放在一个公共文件中,并在每个asm文件中%include

答案 1 :(得分:0)

;   Included in file for all programs
_write equ 1
_open  equ 2
_close equ 3
;   End include

; Begin code
    mov rax, _open    ; Obvious what you're doing
    syscall           ; Only takes 2 instructions, not 3 and a call

write_loop:
;   set up data pointers for write
    mov rax, _write
    syscall
; check if end of data
    bne write_loop

    mov rax, _close
    syscall