; compile with:

;     NASM -f macho64 ask.asm && ld -macosx_version_min 10.7.0 -lSystem -o ask ask.o

;

;  Written by Coty Miller 2.Sep.18

;  Written for 64bit MacOS X

global start

section .text


  %macro asmout 2

mov rsi, %1

mov rdx, %2

call print

  %endmacro


start:

asmout msg, msg.len


call readkbd


mov rcx, 0

.count:

mov rdx, name

add rdx, rcx

mov al, [edx]

cmp al, 0

je .exit

inc rcx

jmp .count

.exit:

push rcx

asmout himsg, himsg.len

pop rcx

asmout name, rcx

asmout msg2, msg2.len

exit:

mov rax, ints.exit

mov rdi, 0

syscall


print:

push rax

push rdi

mov rax, ints.write

mov rdi, 1 ; stdout

syscall

pop rdi

pop rax

ret

readkbd:

push rax

push rsi

push rdx

push rdi

mov rax, ints.read

mov rsi, name

mov rdx, 40

mov rdi, 1

syscall

pop rdi

pop rdx

pop rsi

pop rax

ret


section .data

ints:

.exit equ 0x2000001

.read   equ 0x2000003

.write equ 0x2000004


msg db "What is your name?", 10

.len equ $ - msg


himsg db "Hello, "

.len equ $ - himsg


msg2 db "!",10

.len equ 2


name: db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,