Last active
February 12, 2023 09:09
-
-
Save chirag-droid/68f99a3a007258a4d9a5d44d74950743 to your computer and use it in GitHub Desktop.
An assembly implementation of https://www.reddit.com/r/developersIndia/comments/10zo9o2
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
; Linux x86_64 | |
; compile -> nasm -felf64 main.asm -o main.o | |
; link -> ld main.o -o main | |
; Syscalls NR numbers ;unistd_64.h | |
%define SYS_READ 0 | |
%define SYS_WRITE 1 | |
%define SYS_EXIT 60 | |
%define STDIN 0 | |
%define STDOUT 1 | |
section .rodata | |
ask_chutiya: db "are you chutiya? " | |
ask_chutiya_len equ $-ask_chutiya | |
yes: db "yes", 0 | |
no: db "no", 0 | |
honest_chutiya: db "good job on being honest chutiya", 0xA | |
honest_chutiya_len equ $-honest_chutiya | |
maha_chutiya: db "then you are a Maha chutiya", 0xA | |
maha_chutiya_len equ $-maha_chutiya | |
dumb_chutiya: db "chuitya hai kya itna kuch type kr diya?", 0xA | |
dumb_chutiya_len equ $-dumb_chutiya | |
newline: db 0xA | |
section .bss | |
dummy resd 1 | |
section .data | |
answer: times 4 db 0 | |
lf: dq 4 | |
section .text | |
global _start ; entry point for linker | |
_start: | |
; print ask chutiya string | |
mov rdi, ask_chutiya | |
mov rsi, ask_chutiya_len | |
call print ;print | |
%define count 4 | |
; scan answer from user | |
mov rdi, answer ; buffer where we store answer | |
mov rsi, count ; count of buffer to read | |
call scan | |
; store number of bytes read in r12 for future | |
mov r12, count | |
cmp rax, count | |
jb less | |
jmp skip | |
less: | |
mov r12, rax | |
skip: | |
; check number of input with requested count | |
cmp rax, count | |
; if its below OK | |
jb check_answer | |
; if last byte \n OK | |
mov bl, [answer+count-1] | |
cmp bl, 10 | |
je check_answer | |
call dummy_scan | |
dumb: | |
; when someone enters lot of strings | |
mov rdi, dumb_chutiya | |
mov rsi, dumb_chutiya_len | |
call print | |
jmp end | |
check_answer: | |
; change last byte to 0 | |
mov BYTE [answer+r12-1], 0 | |
mov rdi, answer | |
mov rsi, yes | |
call cmpstr | |
je honest | |
mov rdi, answer | |
mov rsi, no | |
call cmpstr | |
je maha | |
jmp end | |
honest: | |
mov rdi, honest_chutiya | |
mov rsi, honest_chutiya_len | |
call print | |
jmp end | |
maha: | |
mov rdi, maha_chutiya | |
mov rsi, maha_chutiya_len | |
call print | |
jmp end | |
end: | |
; exit syscall | |
mov rax, SYS_EXIT | |
xor rdi, rdi ; Exit status -> 0 | |
syscall | |
; scan(*buf, count) | |
scan: | |
; read syscall | |
mov rdx, rsi ; count | |
mov rsi, rdi ; where to store the buffer | |
mov rdi, STDIN ; file descriptor | |
mov rax, SYS_READ ; syscall NR number | |
syscall | |
ret | |
; perform a dummy read of data | |
; until EOF or \n | |
dummy_scan: | |
push rbx | |
ds_loop: | |
mov rdi, dummy | |
mov rsi, 1 | |
call scan | |
; check if we reached EAF | |
test rax, rax | |
jz ds_end | |
cmp DWORD [dummy], 10 | |
jne ds_loop | |
ds_end: | |
pop rbx | |
ret | |
; print(*buf, count) prints to stdout | |
print: | |
; write syscall | |
mov rdx, rsi ; count | |
mov rsi, rdi ; buffer | |
mov rdi, STDOUT ; file descriptor | |
mov rax, SYS_WRITE ; syscall NR number | |
syscall | |
ret | |
; println() prints a new line | |
println: | |
mov rdi, newline ; buffer -> newline | |
mov rsi, 1 ; count 1 | |
call print | |
ret | |
; compare(*buf, *buf) | |
cmpstr: | |
push rbx | |
mov rbx, 0 | |
loop_cmpstr: | |
mov cl, [rdi+rbx] | |
mov dl, [rsi+rbx] | |
cmp cl, dl | |
je check_null | |
jne end_cmpstr | |
check_null: | |
test cl, cl | |
jz end_cmpstr | |
inc rbx | |
jmp loop_cmpstr | |
end_cmpstr: | |
pop rbx | |
ret |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment