Created
December 5, 2012 02:00
-
-
Save leopic/4211411 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; constants | |
section .data | |
lblZero: db 'Zero'; | |
lblOne: db 'One '; | |
lblNumLength: db 0x4; | |
lblErr: db 'There was an error.', 10; | |
lblErrLength: equ $ - lblErr; | |
lblInput: db 'Type a single digit number: ', 10; | |
lblInputLength: equ $ - lblInput; | |
tmp: db 0; | |
lblByeBye: db 'Good bye!', 10 ; | |
lblByeByeLength: equ $ - lblByeBye; | |
; code | |
section .text | |
global _start | |
; linker needs this, 'main' | |
_start: | |
loop: | |
; prompt user for a digit ; sys_write | |
mov rax, 1 ; | |
mov rdi, 1 ; | |
mov rsi, lblInput ; | |
mov rdx, lblInputLength ; | |
syscall | |
; store user's input ; read | |
mov rax, 0 ; | |
mov rbx, 19 ; | |
mov rcx, tmp ; | |
mov rdx, 10 ; | |
syscall | |
; series of IFs | |
cmp rcx, 0x0 ; is input 0? 00 exits the program | |
je isZero | |
cmp rcx, '1' ; is input 1? | |
je isOne | |
jmp exit ; case default | |
; user typed 0 | |
isZero: | |
inc rcx ; flag for 0 | |
cmp rcx, 2 ; checking if this is the 2nd zero | |
je exit ; if so, we are outta here | |
mov rsi, lblZero ; | |
mov rcx, -1 ; | |
jmp print ; | |
; user typed 1 | |
isOne: | |
mov rsi, lblOne ; | |
mov rcx, -1 ; | |
jmp print ; | |
; prints the string into the screen ; sys_write | |
print: | |
mov rax, 1 ; | |
mov rdi, 1 ; | |
mov rdx, lblNumLength ; | |
syscall | |
jmp loop | |
; displays an error message | |
err: | |
mov rax, 1 ; | |
mov rdi, 1 ; | |
mov rsi, lblErr ; | |
mov rdx, lblErrLength ; | |
syscall | |
jmp loop | |
; shutsdown program ; sys_write ; sys_exit | |
exit: | |
mov rax, 1 ; | |
mov rdi, 1 ; | |
mov rsi, lblByeBye ; | |
mov rdx, lblByeByeLength ; | |
syscall | |
mov rax, 60 | |
mov rdi, 0 | |
syscall |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment