segmentation fault - Assembly - Segfault with subprogram -
i'm trying debug why i'm getting segfault in subprogram.
it happens on ret line @ end of subprogram - in once 0x00 byte reach @ end of sentence.
main :
.data string: .string "aaaaaaaaaaa" endofstring: .space 8 msg: .string "%c occurs %d times \n" .text .global main main: mov $string,%rsi #rsi = string storage mov $0x61, %ah #storage of mov $0x65, %al #storage of e mov $0x69, %bh #storage of mov $0x6f, %bl #storage of o mov $0x75, %ch #storage of u #case mov %ah,%cl #1 byte register cmp later on. mov $0, %rax #initialize count 0 call freq #generate %rax value mov %rax, %rdx #count printf (2nd argument) mov $msg, %rdi #format printf(1st argument) mov %r8, %rsi #ch printf (3rd argument) xor %rax, %rax #reset %rax printf output call printf #print frequency value of ch in string #case e mov %al,%cl mov $0, %rax #initialize count 0 call freq mov %rax, %rdx #count printf (2nd argument) mov $msg, %rdi #format printf(1st argument) mov %r8, %rsi #ch printf (3rd argument) xor %rax, %rax #reset %rax printf output call printf #print frequency value of ch in string #case o mov %bh,%cl mov $0, %rax #initialize count 0 call freq mov %rax, %rdx #count printf (2nd argument) mov $msg, %rdi #format printf(1st argument) mov %r8, %rsi #ch printf (3rd argument) xor %rax, %rax #reset %rax printf output call printf #print frequency value of ch in string #case mov %bl,%cl mov $0, %rax #initialize count 0 call freq mov %rax, %rdx #count printf (2nd argument) mov $msg, %rdi #format printf(1st argument) mov %r8, %rsi #ch printf (3rd argument) xor %rax, %rax #reset %rax printf output call printf #print frequency value of ch in string #case u mov %ch,%cl mov $0, %rax #initialize count 0 call freq mov %rax, %rdx #count printf (2nd argument) mov $msg, %rdi #format printf(1st argument) mov %r8, %rsi #ch printf (3rd argument) xor %rax, %rax #reset %rax printf output call printf #print frequency value of ch in string jmp done done: ret
subprogram:
.text .globl freq freq: #subprogram body start: cmpb $0,8(%rsi) #check end of string je donefreq loopfreq: cmp %cl, 8(%rsi) #compare first string char vowel je incrementstring #if equal - jump increment_string add $1, %rsi #if not - increment string jmp start #jump loop check end of string status/next char incrementstring: add $1, %rsi #increment next string character add $1, %rax #add 1 frequency of character jmp start donefreq: ret
not sure why happens. - wish debugging gave bit more info :(
does have idea why occurs? followed outline of notes callee function i'm @ loss problem in callee
you should not use %rsp
pointer string. corrupting return address in stack ret
instruction tries jump bogus address. use %rsi
or general purpose register. stack pointer not register can use wish.
Comments
Post a Comment