I am teaching myself x86 assembly without the use of a course or structured program. Because of this, I am probably missing something simple and fundamental, but I have yet to find a solution to this as I cannot think of what to Google anymore.
I am trying to expand on the simple "Hello, World!" program by using functions (procedures?) to be able to eventually process user input and print it to the screen. I thought I have a good understanding of the stack, stack pointer, and the opcodes I have used, but evidently, I do not.
Here is my code:
[BITS 32]
section .data
helloWorld db "Hello, World!",0x0a
section .text
global _start
exit:
mov eax,1 ; exit
mov ebx,0 ; Return 0
int 80h ; Call kernel
strLen: ; String length is in ecx by the end
push edi ; Save edi
push ebp ; Save old base pointer
xor ecx,ecx ; Clear ecx
mov edi,[ebp + 8] ; Skip over return address to get to string
parameter
not ecx ; Sets ecx to highest value (4,294,967,295 or -1)
sub al,al ; Clear al
cld ; Clear direction flag
repne scasb ; Scans string starting at address in edi for NUL
; As it scans, decrements ecx and increments edi
not ecx ; ecx contains string length plus NUL
dec ecx ; Get rid of terminating NUL
pop ebp ; Restore base pointer
pop edi ; Restore edi
ret ; Return to location where called
printStr:
push ebp ; Save old base pointer
mov ebp,esp ; Save stack pointer as base pointer
; ebp + 0 == old ebp
; ebp + 4 == return address
; ebp + 8 == parameter
mov eax,4
mov ebx,1
mov edx,[ebp + 8] ; Move string parameter to edx
push edx ; String parameter for strLen
call strLen ; Find string length
mov edx,ecx ; Move string length to edx
mov ecx,[ebp + 8] ; String to be printed is parameter
int 80h ; Call kernel
pop ebp ; Restore old base pointer
ret ; Return to location where called
_start:
push helloWorld
call printStr
call exit
I think my problem is somewhere with the [ebp + 8] to obtain the first parameter for the functions, but I am not sure why this would be an error. I'll admit I do not really know why I am using the brackets, and I have not found an explanation yet. I have tried many different combinations to remedy this, including trying to move the address there to a different register. I have gotten "Segmentation Fault (core dumped)" with NASM and ld on the Ubuntu subsystem within Windows 10 with an i5-6600k and "Invalid memory reference (SIGSEGV)" on rextester.com, which I use when I do not have access to Linux.