...
This commit is contained in:
@@ -1,63 +1,61 @@
|
||||
.intel_syntax noprefix
|
||||
.extern main
|
||||
.section .text
|
||||
|
||||
.global _start
|
||||
.equ SYS_EXIT, 60
|
||||
|
||||
.text
|
||||
.globl _start
|
||||
_start:
|
||||
# On entry, the stack contains:
|
||||
# [rsp] = argc (argument count)
|
||||
# [rsp+8] = argv[0] (program name)
|
||||
# [rsp+16] = argv[1] (first argument)
|
||||
# ...
|
||||
|
||||
# Get argc from stack
|
||||
mov rdi, [rsp] # rdi = argc
|
||||
|
||||
# Calculate space needed for our array structure
|
||||
# We need: 8 bytes (length) + argc * 8 bytes (pointers)
|
||||
mov rax, rdi # rax = argc
|
||||
shl rax, 3 # rax = argc * 8 (each pointer is 8 bytes)
|
||||
add rax, 8 # rax = 8 + argc * 8 (add space for length)
|
||||
|
||||
# Allocate space on stack (align to 16 bytes)
|
||||
add rax, 15 # Round up to nearest 16
|
||||
and rax, -16 # Align to 16 bytes
|
||||
sub rsp, rax # Allocate space
|
||||
|
||||
# Store array length at beginning
|
||||
mov [rsp], rdi # Store argc as array length
|
||||
|
||||
# Copy argv pointers to our array
|
||||
lea rsi, [rsp + 8] # rsi points to start of argv in stack
|
||||
lea rdi, [rsp + 8] # rdi points to our array data (after length)
|
||||
mov rcx, [rsp] # rcx = argc (loop counter)
|
||||
|
||||
copy_loop:
|
||||
test rcx, rcx # Check if we're done
|
||||
jz done_copying
|
||||
|
||||
mov rax, [rsi] # Load argv[i] pointer
|
||||
mov [rdi], rax # Store in our array
|
||||
add rsi, 8 # Move to next argv entry
|
||||
add rdi, 8 # Move to next array slot
|
||||
dec rcx # Decrement counter
|
||||
jmp copy_loop
|
||||
|
||||
done_copying:
|
||||
# Now rsp points to our array: [length][ptr0][ptr1]...[ptrN-1]
|
||||
mov rdi, rsp # Pass array pointer to main
|
||||
mov rdi, [rsp] # rdi = argc
|
||||
|
||||
# Calculate the size of the array
|
||||
mov rax, rdi # Start with argc
|
||||
shl rax, 3 # Multiply argc by 8
|
||||
add rax, 8 # Add space for the array size
|
||||
|
||||
# Allocate array size on the stack (aligned to 16 bytes)
|
||||
add rax, 15
|
||||
and rax, -16
|
||||
sub rsp, rax
|
||||
|
||||
# Store number of elements at the start of the array
|
||||
mov [rsp], rdi
|
||||
|
||||
mov rcx, rdi # rcx = loop counter
|
||||
lea rsi, [rsp + rax + 8] # rsi = argv[0]
|
||||
lea rdi, [rsp + 8] # rdi = destination_array[0]
|
||||
|
||||
convert_loop:
|
||||
test rcx, rcx
|
||||
jz done_converting
|
||||
|
||||
# Convert current cstring using nub_cstring_to_string
|
||||
push rcx # Save loop counter
|
||||
push rsi # Save argv[i]
|
||||
push rdi # Save destination_array[i]
|
||||
|
||||
mov rdi, [rsi] # Load current argv[i] (cstring)
|
||||
call nub_cstring_to_string
|
||||
|
||||
pop rdi # Restore destination pointer
|
||||
pop rsi # Restore argv pointer
|
||||
pop rcx # Restore loop counter
|
||||
|
||||
test rax, rax
|
||||
jz conversion_failed
|
||||
|
||||
# Store converted string pointer in our array
|
||||
mov [rdi], rax # Store converted string pointer
|
||||
add rdi, 8 # Move to next destination_array entry
|
||||
add rsi, 8 # Move to next argv entry
|
||||
dec rcx # Decrement counter
|
||||
jmp convert_loop
|
||||
|
||||
done_converting:
|
||||
mov rdi, rsp
|
||||
call main
|
||||
|
||||
# Clean up stack (restore original rsp)
|
||||
# Calculate how much we allocated
|
||||
mov rdi, [rsp] # Get argc back
|
||||
shl rdi, 3 # argc * 8
|
||||
add rdi, 8 # + 8 for length
|
||||
add rdi, 15 # Round up
|
||||
and rdi, -16 # Align
|
||||
add rsp, rdi # Restore stack
|
||||
|
||||
# Exit with main's return value
|
||||
mov rdi, rax # rax contains main's return value
|
||||
mov rax, 60 # sys_exit
|
||||
mov rdi, rax
|
||||
mov rax, SYS_EXIT
|
||||
syscall
|
||||
|
||||
conversion_failed:
|
||||
call nub_panic
|
||||
|
||||
Reference in New Issue
Block a user