Sally
Sally

Reputation: 99

segmentation error in assembly code

I'm writing a code to display a string along with it's capitalized equivalent. Here what I have so far:

extern printf

SECTION .data
string1: db "hello",0
fmt1: db "%s",10,0

SECTION .text

global main
main:

   enter 0,0

   ; display string1 as is
   ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
   push string1  ; address of the string
   push fmt1
   call printf
   add esp, 8

   ; capitalize string1
   mov eax, string1            ; convert the 1st letter
   mov ebx, 0 
   mov ebx, [eax]
   sub ebx, 'a'
   add ebx, 'A'
   mov [eax], ebx 

   ; display capitalized  string1
   ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
   push string1  ; address of the string
   push fmt1
   call printf
   add esp, 8

   popa
   leave
   ret

When I execute this I get this output:

hello
Hello
Segmentation Fault

can you help me understand what am I doing wrong?

Upvotes: 2

Views: 61

Answers (1)

Steven
Steven

Reputation: 604

In the end, you "popa" which means pop all registers. You lost your return address for exit.

In the beginning, "pusha" (push all) in order to save the return address in the stack.

Enter 0,0
Pusha

Upvotes: 2

Related Questions