ebvtrnog
ebvtrnog

Reputation: 4397

Syntax formatting in assembler

Is it possible to simplify this code somehow? This is the general pattern for many of my functions. I don't feel like making that global line every time.

global write
write:      mov rax, 1
            syscall
            ret

global open
open:       mov rax, 2
            syscall
            ret

global close
close:      mov rax, 3
            syscall
            ret

EDIT

nasm, 64 bit

Upvotes: 1

Views: 73

Answers (2)

Mike
Mike

Reputation: 2761

;   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

Upvotes: 0

Jester
Jester

Reputation: 58822

You can create a macro, for example:

%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

You can put your macro definitions into a single common file, and %include it in every asm file.

Upvotes: 2

Related Questions