diff --git a/asmatrix.asm b/asmatrix.asm index 19c37e3..9f9ddca 100644 --- a/asmatrix.asm +++ b/asmatrix.asm @@ -1,6 +1,10 @@ global main +; ------------------------------------------------- ; imports +; ------------------------------------------------- + +; ncurses extern initscr extern cbreak extern noecho @@ -11,16 +15,59 @@ extern endwin extern keypad extern stdscr +; libc +extern fopen +extern fread +extern fclose +extern strerror +%ifdef MACOS +extern __error +%else +extern __errno_location +%endif + section .data - ; msg = ["a", "s", "s", "\n" (10), "\0" (0)] - msg db "ass", 10, 0 + rmode db "r", 0 + err_msg db "Error: %s", 10, 0 + usage db "Usage: %s ", 10, 0 + +section .bss + ; 4KB buffer + buffer resb 4096 section .text default rel +; ------------------------------------------------- +; Utility function: print string to screen +; rdi = string address +; ------------------------------------------------- +print_screen: + ; since printw is a variadic function, + ; we must declare number of registers used. + ; xor eax, eax := 0 since we are using + ; printw with 0 arguments + xor eax, eax + jmp printw + +; ------------------------------------------------- +; Main entry point: program initialization and execution +; rdi = argc (argument count) +; rsi = argv (argument vector) +; ------------------------------------------------- main: - ; stack alignment + ; save argc/argv + push rbp + mov rbp, rsp + + ; Save argc and argv in callee-saved registers + mov r12, rdi ; r12 = argc + mov r13, rsi ; r13 = argv + + ; stack alignment (16-byte align before calls) sub rsp, 8 + push r12 + push r13 ; initialise the screen call initscr @@ -37,20 +84,86 @@ main: mov esi, 1 call keypad - ; load address of msg - lea rdi, [msg] + ; ------------------------------------------------- + ; Check arguments + ; ------------------------------------------------- + ; argc == 2? + cmp r12, 2 + jne .show_usage - ; since printw is a variadic function, - ; we must declare number of registers used. - ; xor eax, eax := 0 since we are using - ; printw with 0 arguments + ; ------------------------------------------------- + ; Open File: fopen(argv[1], "r") + ; ------------------------------------------------- + mov rdi, [r13 + 8] ; rdi = argv[1] + lea rsi, [rmode] ; rsi = "r" + call fopen + test rax, rax ; Check for NULL + jz .show_strerror ; If null, display error + mov rbx, rax ; Save FILE* in rbx + + ; ------------------------------------------------- + ; Read File: fread(buffer, 1, 4096, file) + ; ------------------------------------------------- + lea rdi, [buffer] + mov rsi, 1 + mov rdx, 4096 + mov rcx, rbx + call fread + + ; Null-terminate the string + mov rcx, rax ; Save bytes read + lea rdi, [buffer] ; Load buffer address + mov byte [rdi + rcx], 0 ; Null terminate using saved count + + ; Close file + mov rdi, rbx + call fclose + + ; ------------------------------------------------- + ; Print content and exit + ; ------------------------------------------------- + lea rdi, [buffer] + call print_screen + jmp .cleanup + +; ------------------------------------------------- +; Utility function: display usage message +; Shows program usage instructions when arguments are invalid +; ------------------------------------------------- +.show_usage: + lea rdi, [usage] + mov rsi, [r13] ; argv[0] xor eax, eax call printw + jmp .cleanup + +; ------------------------------------------------- +; Utility function: display system error message +; Retrieves errno value and displays corresponding error string +; ------------------------------------------------- +.show_strerror: +%ifdef MACOS + call __error ; rax = &errno +%else + call __errno_location ; rax = &errno +%endif + mov edi, [rax] ; edi = errno value + call strerror ; rax = error string + lea rdi, [err_msg] + mov rsi, rax + xor eax, eax + call printw + +; ------------------------------------------------- +; Utility function: cleanup and exit +; Handles ncurses cleanup, waits for user input, and exits +; Restores stack and returns with exit code 0 +; ------------------------------------------------- +.cleanup: call refresh call getch call endwin - - ; restore stack pointer - add rsp, 8 xor eax, eax + add rsp, 24 ; Clean up stack (8 + 16 for pushed registers) + pop rbp ret diff --git a/assets/hackers-manifesto.txt b/assets/hackers-manifesto.txt new file mode 100644 index 0000000..b693576 --- /dev/null +++ b/assets/hackers-manifesto.txt @@ -0,0 +1,56 @@ +Another one got caught today, it's all over the papers. "Teenager +Arrested in Computer Crime Scandal", "Hacker Arrested after Bank Tampering"... + Damn kids. They're all alike. + + But did you, in your three-piece psychology and 1950's technobrain, +ever take a look behind the eyes of the hacker? Did you ever wonder what +made him tick, what forces shaped him, what may have molded him? + I am a hacker, enter my world... + Mine is a world that begins with school... I'm smarter than most of +the other kids, this crap they teach us bores me... + Damn underachiever. They're all alike. + + I'm in junior high or high school. I've listened to teachers explain +for the fifteenth time how to reduce a fraction. I understand it. "No, Ms. +Smith, I didn't show my work. I did it in my head..." + Damn kid. Probably copied it. They're all alike. + + I made a discovery today. I found a computer. Wait a second, this is +cool. It does what I want it to. If it makes a mistake, it's because I +screwed it up. Not because it doesn't like me... + Or feels threatened by me... + Or thinks I'm a smart ass... + Or doesn't like teaching and shouldn't be here... + Damn kid. All he does is play games. They're all alike. + + And then it happened... a door opened to a world... rushing through +the phone line like heroin through an addict's veins, an electronic pulse is +sent out, a refuge from the day-to-day incompetencies is sought... a board is +found. + "This is it... this is where I belong..." + I know everyone here... even if I've never met them, never talked to +them, may never hear from them again... I know you all... + Damn kid. Tying up the phone line again. They're all alike... + + You bet your ass we're all alike... we've been spoon-fed baby food at +school when we hungered for steak... the bits of meat that you did let slip +through were pre-chewed and tasteless. We've been dominated by sadists, or +ignored by the apathetic. The few that had something to teach found us will- +ing pupils, but those few are like drops of water in the desert. + + This is our world now... the world of the electron and the switch, the +beauty of the baud. We make use of a service already existing without paying +for what could be dirt-cheap if it wasn't run by profiteering gluttons, and +you call us criminals. We explore... and you call us criminals. We seek +after knowledge... and you call us criminals. We exist without skin color, +without nationality, without religious bias... and you call us criminals. +You build atomic bombs, you wage wars, you murder, cheat, and lie to us +and try to make us believe it's for our own good, yet we're the criminals. + + Yes, I am a criminal. My crime is that of curiosity. My crime is +that of judging people by what they say and think, not what they look like. +My crime is that of outsmarting you, something that you will never forgive me +for. + + I am a hacker, and this is my manifesto. You may stop this individual, +but you can't stop us all... after all, we're all alike.