Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
; MACRO: linux print implementation
%macro MC_linuxPrintImpl 3
mov ebx, %1 ; | <- print target (1=stdout)
mov eax, 4 ; Print 100 bytes starting from str
mov ecx, %2 ; | <- source buffer
mov edx, %3 ; | <- number of characters to print
int 80h ; \___
%endmacro
; MACRO: read from terminal
%macro MC_linuxTermRead 2
mov eax, 3 ; Read user input into str
mov ebx, 0 ; | <- read source (0=stdin)
mov ecx, %1 ; | <- destination buffer
mov edx, %2 ; | <- max buffer length
int 80h ; \___
%endmacro
; MACRO: get read string's length and put it in eax
%macro MC_linuxGetReadStrLen 1
mov %1, eax
%endmacro
; MACRO: read from terminal and get string length
%macro MC_linuxTermReadWithLen 3
MC_linuxTermRead %1, %2
MC_linuxGetReadStrLen %3
%endmacro
; MACRO: print to terminal
%macro MC_linuxTermPrint 2
MC_linuxPrintImpl 1, %1, %2
%endmacro
; MACRO: print to file
%macro MC_linuxFileWrite 2
MC_linuxPrintImpl eax, %1, %2
%endmacro
; MACRO: exit program
%macro MC_exitProgram 1
mov eax, 1 ; Return
mov ebx, %1 ; | <- return code
int 80h ; \___
%endmacro
; MACRO: create a file
%macro MC_linuxFileCreate 2
mov eax, 8 ; Create file
mov ebx, %1 ; | <- file path
mov ecx, %2 ; | <- permissions
int 80h ; \___
%endmacro
; MACRO: open a file
%macro MC_linuxFileOpen 3
mov eax, 5 ; Open file
mov ebx, %1 ; | <- file path
mov ecx, %2 ; | <- method (0=RDONLY, 1=WRONLY, 2=RDWR, ...)
mov edx, %3 ; | <- permissions
int 80h ; \___
%endmacro
; MACRO: close last opened file
%macro MC_linuxFileClose 0
mov eax, 6 ; Close file
int 80h ; \___
%endmacro
global _start
; SECTION: initialized data
section .data
strBuffer: times 100 db 0 ; strBuffer is a buffer of 100 '0' bytes
lineFeed: db 10 ; lineFeed is the character `\n` (newline)
filePath: db "./testOutput.txt", 0 ; filePath
; SECTION: uninitialized data
section .bss
strLen resd 1 ; Will contain the entered string length
tmpVar resd 1 ; Temp variable
; SECTION: code
section .text
_start:
MC_linuxTermReadWithLen strBuffer, 100, [strLen]
MC_linuxTermPrint strBuffer, [strLen]
MC_linuxFileCreate filePath, 0777o
MC_linuxFileOpen filePath, 2, 0777o
MC_linuxFileWrite strBuffer, [strLen]
MC_linuxFileClose
MC_exitProgram 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.