汇编代码中的分段错误

时间:2015-12-13 01:35:48

标签: assembly x86

我正在编写一个代码来显示一个字符串以及它的大写等价物。这就是我到目前为止所做的:

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

当我执行此操作时,我得到了这个输出:

hello
Hello
Segmentation Fault
你可以帮我理解我做错了吗?

1 个答案:

答案 0 :(得分:2)

最后,你" popa"这意味着弹出所有寄存器。您丢失了退货地址。

一开始," pusha" (全部推送)以便在堆栈中保存返回地址。

img {
  -webkit-filter: grayscale(1);
  filter: grayscale(1);
}
相关问题