12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- ;;; Required packages: nasm, ld
- ;;; compile commands:
- ;;; nasm -f elf 05-userinput.asm -o bin/05-userinput.o
- ;;; ld -m elf_i386 -s -o bin/05-userinput bin/05-userinput.o
- %define SYSCALL_EXIT 1 ; system call sys_exit
- %define SYSCALL_WRITE 4 ; system call sys_write
- %define SYSCALL_READ 3
- %define STDIN 0 ; file descriptor stdin
- %define STDOUT 1 ; file descriptor stdout
- %define NAME_BUFFER_LEN 256
- section .data
- question db "What is your name? ", 0xa
- questionLen equ $ - question
- prompt db "> "
- promptLen equ $ - prompt
- greetings db "Hello, "
- greetingsLen equ $ - greetings
- section .bss
- name resb NAME_BUFFER_LEN ; resb -> Reserve 16 bytes
- section .text
- global _start
- _start:
- call printQuestionAndPrompt
- call scanName
- call printGreetings
- ; exit code
- mov eax, SYSCALL_EXIT ;
- mov ebx, 0 ; 0 - error code
- int 0x80 ; processor interruption to run syscall
- ret
- scanName:
- mov eax, SYSCALL_READ
- mov ebx, STDIN
- mov ecx, name
- mov edx, NAME_BUFFER_LEN
- int 0x80
-
- ret
- printGreetings:
- mov eax, SYSCALL_WRITE
- mov ebx, STDOUT
- mov ecx, greetings ; text
- mov edx, greetingsLen ; text size
- int 0x80 ; processor interruption to run syscall
- mov eax, SYSCALL_WRITE
- mov ebx, STDOUT
- mov ecx, name ; text
- mov edx, NAME_BUFFER_LEN ; text size (16 bytes reserved)
- int 0x80 ; processor interruption to run syscall
- ; name ends with \n
- ret
- printQuestionAndPrompt:
- mov eax, SYSCALL_WRITE
- mov ebx, STDOUT
- mov ecx, question ; text
- mov edx, questionLen ; text size
- int 0x80 ; processor interruption to run syscall
- mov eax, SYSCALL_WRITE
- mov ebx, STDOUT
- mov ecx, prompt ; text
- mov edx, promptLen ; text size
- int 0x80 ; processor interruption to run syscall
- ret
|