Moving and consolidating repos

This commit is contained in:
klein panic
2025-04-14 17:05:27 -04:00
parent 0529e3c972
commit 3a7f7e1e13
36 changed files with 799 additions and 0 deletions

Binary file not shown.

View File

@@ -0,0 +1,34 @@
section .data
msg db 'Result: ', 0 ; declare string, followed by null terminator
newline db 0xA, 0 ; Newline character for format
section .bss
res resb 1 ; reserve 1 byte of memory for storing Result
section .text
global _start ; Define the entry point of the program
_start:
; Initialize values
mov eax, 4 ; Move the value 4 into register eax
mov ebx, 3 ; move the value 3 into register ebx
; Perform the addition
add eax, ebx ; add the values of eax n ebx register, store the result in eax
; Convert the result to ASCII
mov ecx, res ; point ecx to the start of the result buffer
add eax, '0' ; convert the result in eax to ASCII
mov [ecx], al ; Store the ASCII character in the result buffer
mov byte [ecx+1], 0 ; Null terminate the string
; print the string
mov eax, 4 ; Syscall number for sys_write
mov ebx, 1 ; File descripor for stdout
mov ecx, msg ; point to the msg string defined prior
mov edx, 8 ; Length of the message storing
int 0x80 ; Interrupt to invoke syscall, call the kernel
; print the result
mov eax, 4 ; syscall for sys_write
mov ebx, 1 ; file desciptor, stdout
mov ecx, res ; pointer to the result in memory
mov edx, 1 ; length of the result
int 0x80 ; interupt to invoke the systemcall
; exit, cleanly
mov eax, 1 ; syscall for sys_exit
xor ebx, ebx ; set ebx to 0 (return)
int 0x80 ; interupt to invoke the syscall

View File

@@ -0,0 +1,2 @@
nasm -f elf64 -o output.o input.asm
ld input.o -o binary

BIN
x86_64/Hello/hello34 Executable file

Binary file not shown.

18
x86_64/Hello/hello34.asm Normal file
View File

@@ -0,0 +1,18 @@
section .data
hello db "Hello, World!", 10 ; The string to print, with a new line terminator
section .text
global _start ; The entry point for the program
_start:
; Write the string to stdout
mov eax, 4 ; sys_write system call number
mov ebx, 1 ; File descriptor (stdout)
mov ecx, hello ; Pointer to the string
mov edx, 13 ; Length of the string
int 0x80 ; Call the kernel
; Exit the program
mov eax, 1 ; sys_exit system call number
xor ebx, ebx ; Exit code 0
int 0x80 ; Call the kernel

BIN
x86_64/Hello/hello34.o Normal file

Binary file not shown.

BIN
x86_64/Hello/hello64 Executable file

Binary file not shown.

16
x86_64/Hello/hello64.asm Normal file
View File

@@ -0,0 +1,16 @@
section .data
hello db "Hello, World!", 10 ; Declared bits with a new line term (10)
section .text
global _start
_start:
mov rax, 1
mov rdi, 1
mov rsi, hello
mov rdx, 14
syscall
mov rax, 60
mov rdi, 0
syscall

BIN
x86_64/Hello/hello64.o Normal file

Binary file not shown.

BIN
x86_64/Hello/hello64_exe Executable file

Binary file not shown.

BIN
x86_64/Hello/hello64_exe.o Normal file

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,39 @@
sction .data
prompt db 'Please enter your name: ', 0 ; Null-terminated prompt message
promptLen equ $ - prompt ; Length of the prompt message
buffer db 128 dup(0) ; Buffer for the input
bufferLen equ 128 ; Maximum buffer length
section .bss
name resb 128 ; Reserve buffer for name
section .text
global _start
_start:
; Print prompt
mov eax, 4 ; sys_write syscall
mov ebx, 1 ; File descriptor 1 - stdout
mov ecx, prompt ; Pointer to the message to print
mov edx, promptLen ; Message length
int 0x80 ; Call the kernel
; Read input
mov eax, 3 ; sys_read syscall
mov ebx, 0 ; File descriptor 0 - stdin
mov ecx, buffer ; Pointer to the input buffer
mov edx, bufferLen ; Maximum number of bytes to read
int 0x80 ; Call the kernel
; Print greeting
mov eax, 4 ; sys_write syscall
mov ebx, 1 ; File descriptor 1 - stdout
mov ecx, buffer ; Pointer to the buffer to print
; Use the number of bytes read as the length to write
int 0x80 ; Call the kernel
; Exit program
mov eax, 1 ; sys_exit syscall
xor ebx, ebx ; Exit code 0
int 0x80 ; Call the kernel

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,61 @@
section .data
numberone db 'Enter fst number: ', 0 ; Null terminated string
numbertwo db 'Enter snd number: ', 0 ; Null terminated string
numonelen equ $ - numberone ; length of prompt 1
numtwolen equ $ - numbertwo ; length of prompt 2
section .bss
numone resb 128 ; reserve for number 1 input
numtwo resb 128 ; reserve for number 2 input
result resb 128 ; reserve for result of addition
section .text
global _start
_start:
; Number 1 prompt
mov eax, 4 ; sys_write syscall in 32 bit registry eax
mov ebx, 1 ; Stdout syscall in 32 bit registry ebx
mov ecx, numberone ; move null terminated string to 32 bit registry ecx
mov edx, numtwolen ; length of null terminated (value) moved to 32 bit registry edx
int 0x80 ; Interrupt kernel, reset
; Number 1 Read
mov eax, 3 ; Sys_read sysclal in 32 bit registry
mov ebx, 0 ; stdin file descriptor in 32 bit regstry
mov ecx, numone ; pointer for input
mov edx, 128 ; maxomim number of bytes to Read
int 0x80 ; Interrupt kernel, reset
; Number 2 prompt
mov eax, 4
mov ebx, 1
mov ecx, numbertwo
mov edx, numtwolen
int 0x80
; Number 2 Read
mov eax, 3
mov ebx, 0
mov ecx, numtwo
mov edx, 128
int 0x80
; Print result
mov eax, 4 ; Sys_write syscall for 32 bit register eax
mov ebx, 1 ; Stdout
mov ecx, numone
int 0x80
mov eax, 4
mov ebx, 1
mov ecx, numtwo
int 0x80
; exit
mov eax, 1 ; sys_exit
xor ebx, ebx ; exit with code 0
int 0x80

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,49 @@
section .data
prompt db "What is your name? ", 10
output1 db "Hello, "
section .bss
input resb 34
section .text
global _start
_start:
call _getInput
call _printOutput1
call _printInput
mov rax, 60
mov rdi, 0
syscall
_getInput:
mov rax, 0
mov rdi, 0
mov rsi, input
mov rdx, 34 ; related to resb
syscall
ret
_printPrompt:
mov rax, 1
mov rdi, 1
mov rsi, prompt
mov rdx, 20 ; change for size of bytes reserved
syscall
ret
_printOutput1:
mov rax, 1
mov rdi, 1
mov rsi, output1
mov rdx, 7 ; change for size of db
syscall
ret
_printInput:
mov rax, 1
mov rdi, 1
mov rsi, input
mov rdx, 34 ; change for resb
syscall
ret

Binary file not shown.

BIN
x86_64/Math-Operations/add/add Executable file

Binary file not shown.

View File

@@ -0,0 +1,221 @@
;----------------------------------------------------------------------------
; Author: SL
;----------------------------------------------------------------------------
[GLOBAL mystart] ; export the start address
;----------------------------------------------------------------------------
[SECTION .text]
;----------------------------------------------------------------------------
; code belongs in this section starting here
;----------------------------------------------------------------------------
; MESSAGE is a macro displays a message for prompts.
; The address of the $ terminated string is passed as the only parameter.
%macro MESSAGE 1
push eax ; macro transparency
push edx
mov ah, 09h ; select write string function
mov edx, %1 ; load EDX with address of string
int 0f1h ; call OS interrupt 0F1H
pop edx ; restore registers
pop eax
%endmacro
mystart:
;----------------------------------------------------------------------------
; MAIN starts here
INF:
MESSAGE msg1 ;callGETBASE for inital base
call GETBASE
cmp edi,0 ;check to see if it should quit
je END
MESSAGE msg2 ;enter the number
call ASCIIBIN
cmp edi, 0
je END
push edi
MESSAGE msg3 ;second base
call GETBASE
cmp edi, 0
je END
pop edi
MESSAGE msg5 ;print answer
mov ebx, OBU ;location of string in outbuffer
mov eax, edi ;accum in edi mov to eax
call BINASCII
call CONOUT
jmp INF
END:
ret
GETBASE: mov ecx, 10 ; BASE 10
mov edi, 0 ; set accum to 0
call ASCIIBIN
cmp edi, 2 ;cmp the upper and lower base limit
jb END1
cmp edi, 36
ja END1
mov ecx, edi ; move current base into accum
ret
END1: mov ecx, 0
ret
ASCIIBIN:
mov edi, 0
mov byte [buffer], 32 ; buffer can hold 32 characters
mov byte [buffer+1], 0 ; reuse 0 characters from last input
mov ah, 0ah ; select buffered input function
mov edx, buffer ; put buffer address in EDX
int 0f1h ; call OS interrupt 0F1H
movzx esi, byte [buffer+1] ;load ECX with number of characters read
mov ebx, buffer+2 ;load address of input text into EBX
looop1: mov al, [ebx] ; load 1 byte from ebx address
inner: cmp al, '0'
jb error2
cmp al, '9'
ja error1
sub al, '0' ; subtract '0'
jmp OK
error1: and al, 0DFh ; else and with DF
cmp al, 'A'
jb error2
cmp al, 'Z'
ja error2
sub al,'A'-10 ; sub al"A"-10
jmp OK
error2: MESSAGE msg4
mov edi, 0 ;zeros out the accumulator
ret
OK: ;compare digit to base. If >, ERROR.
movzx eax, al ; zero extend digit
cmp eax, ecx ; cmp digit to base
ja error2
imul edi, ecx ; accum * base
add edi, eax ; accum + digit
next: add ebx, 1 ; increment ebx
sub esi, 1 ; decraments esi
cmp esi, 0
ja looop1 ; loop if it is above 0
cmp edi, 2147483647 ;checks the upperbound
ja error2
cmp edi, 1 ;check lowerbound
je error2
cmp edi, -2147483647 ; check lower bound
je error2
cmp edi, -1 ;check upperbound
je error2
ret
;----------------------------------------------------------------------------
; CONOUT transfers byte characters from [EBX] to screen (console).
; String at [EBX] must be null terminated.
CONOUT:
push eax ; save regs for subroutine transparency
push edx
push esi
mov esi, ebx ; load ESI with address of buffer
cld ; clear direction flage for forward scan
mov ah, 02h ; select write character function
loop1: lodsb ; get next byte (NB this loop could be
test al, 0ffh ; test for null more efficiently coded!)
jz done
mov dl, al ; copy character into DL for function
int 0f1h ; call OS interrupt 0F1H
jmp loop1 ; ending in JMP is inefficient
done:
pop esi ; restore registers - transparency
pop edx
pop eax
ret
; BINASCII routine to convert a 2's complement number passed in EAX, base in
; ECX, into a null-terminated string of ASCII characters stored at [EBX].
BINASCII:
push edx ; transparency
push edi
mov edi, ebx ; put pointer to output string in EDI
test eax, 0ffffffffh
jns positive
mov byte [edi], '-' ; store minus sign in output string
inc edi
neg eax ; will this work OK with 8000000H?
positive:
push dword 0 ; push marker, keeping stack dword aligned
looop:
mov edx, 0 ; NOTE: HAD TO FIX THIS ONE, CUZ THE STUPID
; INTEL CHIP INSISTS ON GENERATING A DIVIDE
; ERROR EXCEPTION IF QUOTIENT TOO BIG!!!
div ecx ; divide base into R:Q where EDX will
; contain the remainder and EAX the
; dividend then quotient
cmp edx, 9
ja letter
add edx, '0' ; most convenient to do ASCII conversion here
; add ASCII '0'
jmp puush ; so we can terminate our popping off stack
letter:
add edx, 'A'-10 ; easily: we can have digits of zero which
; couldn't be distinguished from the marker
puush:
push edx ; push the character
test eax, 0ffffffffh ; if quotient is zero we are done
jnz looop
outloop:
pop eax ; pop digits
mov [edi], al ; and store
inc edi ; the null is also popped and stored
test al, 0ffh ; test for null
jnz outloop
pop edi ; transparency
pop edx
ret
;----------------------------------------------------------------------------
[SECTION .data]
;----------------------------------------------------------------------------
; all initialized data variables and constant definitions go here
msg1 db 13,10,"Enter a base number or Q to quit: ", 13, 10, "$"
msg2 db 13,10,"Enter a Number: ", 13, 10, "$"
msg3 db 13,10,"Enter the desired base: ", 13, 10, "$"
msg4 db 13,10,"INVALID INPUT", 13, 10, "$"
msg5 db 13,10,"Your Result is: ", 13, 10, "$"
;----------------------------------------------------------------------------
[SECTION .bss]
;----------------------------------------------------------------------------
; all uninitialized data elements go here
buffer resb 34 ; buffer to store input characters
OBU resb 80 ; buffer to store output char

BIN
x86_64/stack/stack Executable file

Binary file not shown.

BIN
x86_64/stack/stack.o Normal file

Binary file not shown.

168
x86_64/stack/stask.asm Normal file
View File

@@ -0,0 +1,168 @@
section .data
SYS_WRITE equ 1
STD_IN equ 1
SYS_EXIT equ 60
EXIT_CODE equ 0
NEW_LINE db 0xa
WRONG_ARGC db "Must be two command line argument", 0xa
section .text
global _start
_start:
;; rcx - argc
pop rcx
;;
;; Check argc
;;
cmp rcx, 3
jne argcError
;;
;; start to sum arguments
;;
;; skip argv[0] - program name
add rsp, 8
;; get argv[1]
pop rsi
;; convert argv[1] str to int
call str_to_int
;; put first num to r10
mov r10, rax
;; get argv[2]
pop rsi
;; convert argv[2] str to int
call str_to_int
;; put second num to r11
mov r11, rax
;; sum it
add r10, r11
;;
;; Convert to string
;;
mov rax, r10
;; number counter
xor r12, r12
;; convert to string
jmp int_to_str
;;
;; Print argc error
;;
argcError:
;; sys_write syscall
mov rax, 1
;; file descritor, standard output
mov rdi, 1
;; message address
mov rsi, WRONG_ARGC
;; length of message
mov rdx, 34
;; call write syscall
syscall
;; exit from program
jmp exit
;;
;; Convert int to string
;;
int_to_str:
;; reminder from division
mov rdx, 0
;; base
mov rbx, 10
;; rax = rax / 10
div rbx
;; add \0
add rdx, 48
;; push reminder to stack
push rdx
;; go next
inc r12
;; check factor with 0
cmp rax, 0x0
;; loop again
jne int_to_str
;; print result
jmp print
;;
;; Convert string to int
;;
str_to_int:
;; accumulator
xor rax, rax
;; base for multiplication
mov rcx, 10
next:
;; check that it is end of string
cmp [rsi], byte 0
;; return int
je return_str
;; mov current char to bl
mov bl, [rsi]
;; get number
sub bl, 48
;; rax = rax * 10
mul rcx
;; ax = ax + digit
add rax, rbx
;; get next number
inc rsi
;; again
jmp next
return_str:
ret
;;
;; Print number
;;
print:
;;;; calculate number length
mov rax, 1
mul r12
mov r12, 8
mul r12
mov rdx, rax
;;;;
;;;; print sum
mov rax, SYS_WRITE
mov rdi, STD_IN
mov rsi, rsp
;; call sys_write
syscall
;;
;; newline
jmp printNewline
;;
;; Print number
;;
printNewline:
mov rax, SYS_WRITE
mov rdi, STD_IN
mov rsi, NEW_LINE
mov rdx, 1
syscall
jmp exit
;;
;; Exit from program
;;
exit:
;; syscall number
mov rax, SYS_EXIT
;; exit code
mov rdi, EXIT_CODE
;; call sys_exit
syscall

View File

@@ -0,0 +1,21 @@
section .data
text db "Hello, World!", 10
section .text
global _start
_start:
mov rax, 1
mov rdi, 1
mov rsi, text
mov rdx, 14
syscall
mov rax, 60
mov rdi, 0
syscall

BIN
x86_64/strings/reverse Executable file

Binary file not shown.

123
x86_64/strings/reverse.asm Normal file
View File

@@ -0,0 +1,123 @@
;;
;; initialized data
;;
section .data
SYS_WRITE equ 1
STD_OUT equ 1
SYS_EXIT equ 60
EXIT_CODE equ 0
NEW_LINE db 0xa
INPUT db "Hello world!"
;;
;; non initialized data
;;
section .bss
OUTPUT resb 1
;;
;; code
;;
section .text
global _start
;;
;; main routine
;;
_start:
;; get addres of INPUT
mov rsi, INPUT
;; zeroize rcx for counter
xor rcx, rcx
; df = 0 si++
cld
; remember place after function call
mov rdi, $ + 15
;; get string lengt
call calculateStrLength
;; write zeros to rax
xor rax, rax
;; additional counter for reverseStr
xor rdi, rdi
;; reverse string
jmp reverseStr
;;
;; calculate length of string
;;
calculateStrLength:
;; check is it end of string
cmp byte [rsi], 0
;; if yes exit from function
je exitFromRoutine
;; load byte from rsi to al and inc rsi
lodsb
;; push symbol to stack
push rax
;; increase counter
inc rcx
;; loop again
jmp calculateStrLength
;;
;; back to _start
;;
exitFromRoutine:
;; push return addres to stack again
push rdi
;; return to _start
ret
;;
;; reverse string
;;
;; 31 in stack
reverseStr:
;; check is it end of string
cmp rcx, 0
;; if yes print result string
je printResult
;; get symbol from stack
pop rax
;; write it to output buffer
mov [OUTPUT + rdi], rax
;; decrease length counter
dec rcx
;; increase additional length counter (for write syscall)
inc rdi
;; loop again
jmp reverseStr
;;
;; Print result string
;;
printResult:
mov rdx, rdi
mov rax, 1
mov rdi, 1
mov rsi, OUTPUT
syscall
jmp printNewLine
;;
;; Print new line
;;
printNewLine:
mov rax, SYS_WRITE
mov rdi, STD_OUT
mov rsi, NEW_LINE
mov rdx, 1
syscall
jmp exit
;;
;; Exit from program
;;
exit:
;; syscall number
mov rax, SYS_EXIT
;; exit code
mov rdi, EXIT_CODE
;; call sys_exit
syscall

BIN
x86_64/strings/reverse.o Normal file

Binary file not shown.

BIN
x86_64/sum/sum Executable file

Binary file not shown.

47
x86_64/sum/sum.asm Normal file
View File

@@ -0,0 +1,47 @@
;initialised data section
section .data
; Define constants
num1: equ 100
num2: equ 50
; http://geekswithblogs.net/MarkPearl/archive/2011/04/13/more-nasm.aspx
msg: db "Sum is correct", 10
;;
;; program code
;;
section .text
global _start
;; entry point
_start:
; get sum of num1 and num2
mov rax, num1
mov rbx, num2
add rax, rbx
; compare rax with correct sum - 150
cmp rax, 150
; if rax is not 150 go to exit
jne .exit
; if rax is 150 print msg
jmp .rightSum
; Print message that sum is correct
.rightSum:
;; write syscall
mov rax, 1
;; file descritor, standard output
mov rdi, 1
;; message address
mov rsi, msg
;; length of message
mov rdx, 15
;; call write syscall
syscall
; exit from program
jmp .exit
; exit procedure
.exit:
mov rax, 60
mov rdi, 0
syscall

BIN
x86_64/sum/sum.o Normal file

Binary file not shown.