During the “Total Recall” episodes I’m going to recall the knowledge I used to bury deep in corners of my mind.
Here is a simple output:
[root@asm workspace]# nasm -f elf64 -o hello.o hello.asm && ld hello.o -o hello && ./hello
wait...1
wait...2
wait...3
wait...4
wait...5
Hey,I'm alive!
[root@asm workspace]#
I’m going to share some asm code with comments here as a note:
section .data ; section where you define data before compilation
textWait db "wait...# ",10 ; db = define bytes "wait...#X \n"
textAlive db "Hey,I'm alive!", 10
section .text ; section where you put the code
global _start ; label that is known by the linker.
global _alive
global _addWait
global _continue
_start:
; loop part
inc rcx ; increase rcx - part of the loop for (i=0;i<=5;i++)
; Jump if Lower or equal (like "if then" condition)
cmp rcx ,5
jle _addWait
_alive:
xor rsi, rsi
mov rsi, textAlive ; fill rsi with pointer to "Hi, I'm alive"
jmp _continue
_addWait:
xor rsi,rsi
mov rax, rcx
add rax, '0'; make text symbol
mov rsi, textWait ; fill rsi with " wait..."
mov [rsi+8], al ; fill with COUNTER from ($rcx) as character
_continue:
; execute the syscall and pass the data from 64bit registers
; syscall to write the data to stdout.
push rcx ; save rax to the stack
xor rax,rax ; set rax to 0
mov rax, 1 ; sys_write
mov rdi, 1 ; stdout
;rsi register is filled in "_alive" and "_addWait"
mov rdx, 15 ; buf len
syscall; syscall takes rax, rdi, rsi, rdx
; loop part
pop rcx ; get looddp's "i" value from the stack
cmp rcx, 5 ; compare rax and 5
jle _start ; if smaller/equal to 5
; syscall to exit the logic with argument
mov rax, 60 ; sys_exit
mov rdi, 0 ; return 0
syscall
Dmytro.