段错误(核心哑)x86 汇编 AT&T 语法
Segment fault (core dumbed) x86 Assembly AT&T syntax
我正在尝试编写一个包含 3 个数组的代码,return 最后一个数组中的最大数量(是的,类似于 ProgramminGroundUp 书中的内容)但我希望函数根据数组大小退出不是当它到达元素零时
但是代码给了我
Segment fault (Core Dumped)
我使用 'as' 汇编程序'和 gnu 加载程序 'ld'
这是完整的代码
.section .data
first_data_items:
.long 48,65,49,25,36
second_data_items:
.long 123,15,48,67,25,69
third_data_items:
.long 102,120,156,32,14,78,100
.section .text
.globl _start
_start:
pushl $first_data_items
pushl
call max
addl , %esp
pushl $second_data_items
pushl
call max
addl , %esp
pushl $third_data_items
pushl
call max
addl , %esp
movl %eax, %ebx
movl , %eax
int [=11=]x80
.type max, @function
max:
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %ecx #ecx will be the size of the array
movl 12(%ebp), %ebx #ebx will be the base pointer
movl [=11=], %edi #edi will be the index
movl 0(%ebx, %edi, 4), %eax #eax will hold the maximum number
start_loop:
cmpl [=11=], %ecx
je end_loop
incl %edi
movl 0(%ebx, %edi,4), %esi #esi will hold the current element
cmpl %eax, %esi
jle start_loop
movl %esi, %eax
decl %ecx
jmp start_loop
end_loop:
movl %ebp, %esp
popl %ebp
ret
我在比较语句之后移动了 decl %ecx
(感谢 Michael Petch),使 ecx 在每个循环中递减 1
所以代码将是
.section .data
first_data_items:
.long 48,65,49,25,36
second_data_items:
.long 123,15,48,67,25,69
third_data_items:
.long 102,120,156,32,14,170,100
.section .text
.globl _start
_start:
pushl $first_data_items
pushl
call max
addl , %esp
pushl $second_data_items
pushl
call max
addl , %esp
pushl $third_data_items
pushl
call max
addl , %esp
movl %eax, %ebx
movl , %eax
int [=10=]x80
.type max, @function
max:
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %ecx #ecx will be the size of the array
movl 12(%ebp), %ebx #ebx will be the base pointer
movl [=10=], %edi #edi will be the index
movl 0(%ebx, %edi, 4), %eax #eax will hold the maximum number
start_loop:
cmpl [=10=], %ecx
je end_loop
decl %ecx
incl %edi
movl 0(%ebx, %edi,4), %esi #esi will hold the current element
cmpl %eax, %esi
jle start_loop
movl %esi, %eax
jmp start_loop
end_loop:
movl %ebp, %esp
popl %ebp
ret
关于我对 lodsd
用法的评论,max
函数示例:
.type max, @function
max:
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %ecx # ecx will be the size of the array
movl 12(%ebp), %esi # esi will be the base pointer
popl %ebp # stack_frame usage complete, restore ebp
lea (%esi,%ecx,4), %ecx # ecx = (base pointer + 4*size) (end() ptr)
mov [=10=]x80000000,%edi # current max = INT_MIN
start_loop:
cmp %ecx, %esi
jae end_loop # end() ptr reached (esi >= ecx)
lodsl # eax = [esi+=4]
cmp %edi, %eax # check if it is new max
cmovg %eax, %edi # update max as needed (eax > edi)
jmp start_loop # go through whole array
end_loop:
mov %edi, %eax # eax = current_max
ret
(CMOVcc
需要 i686+ 目标架构)
我正在尝试编写一个包含 3 个数组的代码,return 最后一个数组中的最大数量(是的,类似于 ProgramminGroundUp 书中的内容)但我希望函数根据数组大小退出不是当它到达元素零时 但是代码给了我
Segment fault (Core Dumped)
我使用 'as' 汇编程序'和 gnu 加载程序 'ld' 这是完整的代码
.section .data
first_data_items:
.long 48,65,49,25,36
second_data_items:
.long 123,15,48,67,25,69
third_data_items:
.long 102,120,156,32,14,78,100
.section .text
.globl _start
_start:
pushl $first_data_items
pushl
call max
addl , %esp
pushl $second_data_items
pushl
call max
addl , %esp
pushl $third_data_items
pushl
call max
addl , %esp
movl %eax, %ebx
movl , %eax
int [=11=]x80
.type max, @function
max:
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %ecx #ecx will be the size of the array
movl 12(%ebp), %ebx #ebx will be the base pointer
movl [=11=], %edi #edi will be the index
movl 0(%ebx, %edi, 4), %eax #eax will hold the maximum number
start_loop:
cmpl [=11=], %ecx
je end_loop
incl %edi
movl 0(%ebx, %edi,4), %esi #esi will hold the current element
cmpl %eax, %esi
jle start_loop
movl %esi, %eax
decl %ecx
jmp start_loop
end_loop:
movl %ebp, %esp
popl %ebp
ret
我在比较语句之后移动了 decl %ecx
(感谢 Michael Petch),使 ecx 在每个循环中递减 1
所以代码将是
.section .data
first_data_items:
.long 48,65,49,25,36
second_data_items:
.long 123,15,48,67,25,69
third_data_items:
.long 102,120,156,32,14,170,100
.section .text
.globl _start
_start:
pushl $first_data_items
pushl
call max
addl , %esp
pushl $second_data_items
pushl
call max
addl , %esp
pushl $third_data_items
pushl
call max
addl , %esp
movl %eax, %ebx
movl , %eax
int [=10=]x80
.type max, @function
max:
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %ecx #ecx will be the size of the array
movl 12(%ebp), %ebx #ebx will be the base pointer
movl [=10=], %edi #edi will be the index
movl 0(%ebx, %edi, 4), %eax #eax will hold the maximum number
start_loop:
cmpl [=10=], %ecx
je end_loop
decl %ecx
incl %edi
movl 0(%ebx, %edi,4), %esi #esi will hold the current element
cmpl %eax, %esi
jle start_loop
movl %esi, %eax
jmp start_loop
end_loop:
movl %ebp, %esp
popl %ebp
ret
关于我对 lodsd
用法的评论,max
函数示例:
.type max, @function
max:
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %ecx # ecx will be the size of the array
movl 12(%ebp), %esi # esi will be the base pointer
popl %ebp # stack_frame usage complete, restore ebp
lea (%esi,%ecx,4), %ecx # ecx = (base pointer + 4*size) (end() ptr)
mov [=10=]x80000000,%edi # current max = INT_MIN
start_loop:
cmp %ecx, %esi
jae end_loop # end() ptr reached (esi >= ecx)
lodsl # eax = [esi+=4]
cmp %edi, %eax # check if it is new max
cmovg %eax, %edi # update max as needed (eax > edi)
jmp start_loop # go through whole array
end_loop:
mov %edi, %eax # eax = current_max
ret
(CMOVcc
需要 i686+ 目标架构)