I am trying to learn assembly, and have a program in AT&T syntax, for use with GNU AS Which I believe should work. I receive this error with GDB:
Program received signal SIGSEGV, Segmentation fault.
.PROGRAM () at concatenator.s:60
60 call strlen
Current language: auto; currently asm
The code is:
.file "concatenator.s"
.globl _start
.section .text
strlen:
mov %esp, (str1)
push %ebx
push %ecx
push %edx
mov $1, %edi
sub %ecx, %ecx
sub %al, %al
not %ecx
cld
repne scasb
not %ecx
dec %ecx
mov %ecx, %eax
pop %edx
pop %ecx
pop %ebx
leave
ret
write:
push %eax
push %ebx
push %ecx
push %edx
mov %eax, %ecx
mov $4, %eax
mov $4, %edx
mov $2, %ebx
int $0x80
pop %edx
pop %ecx
pop %ebx
pop %eax
ret
.globl concatenate
concatenate:
pop %eax
mov %eax, (str2)
pop %eax
mov %eax, (str1)
push %ebx
push %ecx
push %edx
pushl %ebp#Pushes Previous programs local vars to the stack.
movl %esp, %ebp
subl $24, %esp
.PROGRAM:
movl (str1), %esp#Moves str1 to ESP
call strlen#//Strlen counts len of ESP
movl %eax, -16(%ebp)#//Moves eax[Return] into ebp[-16](len)
movl $str2, (%esp)#//Moves str2 to ESP
call strlen#//Counts len of ESP
subl $1, %eax#//Removes one from the return value
movl %eax, -12(%ebp)#//Stores return in INT len2
//movl -12(%ebp), %eax
movl %eax, -8(%ebp)#//Stores return in INT J
movl $0, -4(%ebp)##//INT X = 0
jmp .L7
.L8:
addl $1, -8(%ebp)#//ADDS 1 to J
movl -8(%ebp), %eax#//Moves J to EAX
movl -4(%ebp), %edx#//MOVES X TO EDX
movzbl str1(%edx), %edx#//Moves str1[EDX] (EDX is X) to EDX and fills wit null
movb %dl, str2(%eax)#//Moves one byte, (Tbhe character we just copied) into str2 [EAX]
addl $1, -4(%ebp)#//INT X++
.L7:
movl -4(%ebp), %eax#//Moves INT X to EAX
cmpl -16(%ebp), %eax#//Compares len with EAX
jl .L8#//While below length of string one, go to L8 and copy str1 to str2
addl $1, -8(%ebp)#//Adds one to J(J++)
movl -8(%ebp), %eax#//Moves J to EAX
movb $0, str2(%eax)#//Adds null character to string at position J.
.RETURN:
leave
pop %edx
pop %ecx
pop %ebx
mov (str2), %eax
ret
_start:
push str1
push str2
call concatenate
mov %eax, str2
mov $1, %eax
mov $0, %ebx
int $0x80
.globl str1
.section .data
str1:
.string "DEF"
.zero 252
str2:
.string "ABC"
.zero 252
Is there anything I really obviously did wrong?, and which resources would you recommend for learning assembly? (I have read the WikiBooks X86 Assembly Article, and Most of the GAS Manual).