Skip to content

Instantly share code, notes, and snippets.

@leopic
Created December 5, 2012 02:00
Show Gist options
  • Save leopic/4211411 to your computer and use it in GitHub Desktop.
Save leopic/4211411 to your computer and use it in GitHub Desktop.
; 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