diff options
| author | Carlos Maiolino <[email protected]> | 2025-07-10 22:55:07 +0200 |
|---|---|---|
| committer | Carlos Maiolino <[email protected]> | 2025-07-10 22:56:55 +0200 |
| commit | d98f46ce647846b0aa30b2e16a30fd4e152a1bf5 (patch) | |
| tree | 267474fcc77cf20b428f6f4c7f768ca09f4cfe0e /PGU/OLD/chapter3 | |
| parent | 869e68986aa8f69af6e7842260a68d1e5c6f796f (diff) | |
Add new code
Signed-off-by: Carlos Maiolino <[email protected]>
Diffstat (limited to 'PGU/OLD/chapter3')
| -rw-r--r-- | PGU/OLD/chapter3/exit | bin | 0 -> 704 bytes | |||
| -rw-r--r-- | PGU/OLD/chapter3/exit.s | 70 | ||||
| -rw-r--r-- | PGU/OLD/chapter3/max.s | 42 | ||||
| -rw-r--r-- | PGU/OLD/chapter3/min.s | 52 |
4 files changed, 164 insertions, 0 deletions
diff --git a/PGU/OLD/chapter3/exit b/PGU/OLD/chapter3/exit Binary files differnew file mode 100644 index 0000000..a70d0fd --- /dev/null +++ b/PGU/OLD/chapter3/exit diff --git a/PGU/OLD/chapter3/exit.s b/PGU/OLD/chapter3/exit.s new file mode 100644 index 0000000..632ebed --- /dev/null +++ b/PGU/OLD/chapter3/exit.s @@ -0,0 +1,70 @@ +# A useless program, which the only thing this shit does is call exit syscall +# The exit status code can be read using + +# `echo $?` on shell, which will show the exit status of the last program run. + + +# %eax holds the system call number +# %ebx holds the return status + + +# Assembler directives (or pseudo-operations). +# +# Everything starting with a period, is an instruction to the assembler itself. +# They are handled by the assembler program, not actually executed by the +# computer. + +# .section command breaks the program down into sections. + +# .data section lists any memory storage which will be needed for data +.section .data + +# .text section lists the program instructions +.section .text + +# Instructs the assembler that _start is "important to remember". +# _start is a symbol, which is going to be replaced by something else either +# during assembly or linking + +# Symbols are generally used to mark locations of programs or data, so you can +# refer to them by name instead of by location number + +# .globl means that the assembler should not discard this symbol after assembly, +# because the linker will need it. + +# _start is a special symbol that always needs to be marked with .globl, because +# it marks the location of the start of the program. + +# If the program is not marked this way, when the computer loads the program, it +# won't know where to begin running the program. + +.globl _start + +# Defines the value of _start label. Label == a symbol followed by a colon. +# Labels defines a symbol's value. + +# Labels tell the assembler to make the symbol's value be wherever the next +# instruction or data element will be. + +_start: + +# different from the book, I'm using x86_64 instructions and its registers, so, +# instead of using %eax and %ebx, I'm using %rax and %rbx directly. The movl +# instruction needed to be changed to movq (move quad) to adapt to the 64-bit +# registers +# mov: (move) 16-bit +# movl: (move long) 32-bit +# movq: (move quad) 64-bit + +#ins $(val), (dest) == immediate mode +movq $1, %rax # Move value 1 into %eax register + # %eax will hold the syscall number + # by the linux's calling convention + +movq $100, %rbx # Move value 0 into %ebx register + # %ebx will hold the exit() syscall + # argument. + +int $0x80 # Interrupt 0x80 will wake up the kernel + # execute the syscall loaded into %eax + diff --git a/PGU/OLD/chapter3/max.s b/PGU/OLD/chapter3/max.s new file mode 100644 index 0000000..6ebc906 --- /dev/null +++ b/PGU/OLD/chapter3/max.s @@ -0,0 +1,42 @@ +# Given a list of X numbers, find the maximum number of the list and use it as +# the argument of exit() syscall + +# %rbx holds the maximum number through the whole scanning process +# %rax holds the current element being examined +# %rdi holds the currend position in the list +# zero marks the end of the list + +# Data section, now containing some statically created data +.section .data + +# Just a label to refer to the first item in the list +data_items: + # "Type" of memory location to be reserved. In quotes because it just + # says how many bytes should be reserved to each item, not the 'type' + # itself + # Reserves 10 '8byte' (quad) slots consecutive in memory, + .quad 3,10,9,230,66,77,23,66,12,69,0 + +.section .text + +.globl _start + +_start: + movq $0, %rdi + movq data_items(, %rdi,8), %rax + movq %rax, %rbx + + start_loop: + cmpq $0, %rax + je loop_exit + incq %rdi + movq data_items(,%rdi,8), %rax + cmpq %rbx, %rax + jle start_loop + movq %rax, %rbx + jmp start_loop + + + loop_exit: + movq $1, %rax + int $0x80 diff --git a/PGU/OLD/chapter3/min.s b/PGU/OLD/chapter3/min.s new file mode 100644 index 0000000..9179be7 --- /dev/null +++ b/PGU/OLD/chapter3/min.s @@ -0,0 +1,52 @@ +# This code has been originally copied from max.s + +# It has been modified to: +# - Find the smallest value, instead of the largest +# - Use Start/End addresses to delimit the boundaries of the number list +# +# %rax - holds the current element being examined +# %rbx - Holds the smallest number + +.section .data + +# This version has 2 labels, one to point to the start of the list, and another +# to point to the address right after it, so we can use it to search the list +# boundary. + +data_items: + .quad 234,10,9,230,66,77,23,66,101,69,100 +data_end: + +.section .text + +.globl _start + +_start: + +# Using immediate mode with a label, will give you the ADDRESS of the +# instruction/data it points to. Once the label itself contains an ADDRESS, the +# immediate mode will give you the ADDRESS itself. + +# Using direct mode with a label, will give you the DATA into the address the +# label points to. Once the label itself contains an address, the direct mode, +# as expected will give you the DATA into the address pointed by the label. + movq $data_items, %rax + cmpq $data_end, %rax # We need this check here in case + je exit_loop_empty # the items list is empty + movq (%rax), %rbx + + loop_start: + addq $8, %rax # We are using QuadWords, so 8 bytes + cmpq $data_end, %rax + je exit_loop + + cmpq (%rax), %rbx + jle loop_start + movq (%rax), %rbx + jmp loop_start + + exit_loop_empty: + movq $255, %rbx + exit_loop: + movq $1, %rax + int $0x80 |
