【问题标题】:Segmentation fault in my NASM program(I need a bit of help)我的 NASM 程序中的分段错误(我需要一些帮助)
【发布时间】:2014-11-12 21:31:39
【问题描述】:

我的程序产生了分段错误,因此不会从我的数组中打印出最小的数字。我和我的助教一起检查了我的程序,但她只告诉我分段错误的原因可能是数组越界异常。如果有人这么好心,我需要一点帮助来找出我哪里出错了。

section .data                                       ; initilized data segment
matrixA dd  79, 98, 58, 62, 94                      ; 5x5 matrix
        dd  82, 92, 76, 51, 81
        dd  83, 46, 67, 71, 52
        dd  43, 61, 73, 44, 48
        dd  59, 85, 82, 66, 89

msgLow db 'The lowest Value of the Matrix is:', 0x0a ; Lowest Message
lenLow equ $- msgLow


section .bss                                        ; uninitialized data segment
;...

section .text                                       ; program data
global _start                                       ; global scope
_start:                                             ; main function


printMsg:                                           ; Prints msgLow
mov eax, 4
mov ebx, 1
mov ecx, msgLow
mov edx, lenLow
int 0x80

init:
mov edi, 0                                      ; used to track row position
mov esi, 0                                      ; added to edi give a number in the matrixA
mov ecx, 99                                     ; upper limit of lowest number

call outter                                     ; sub routine for finding lowest number

mov eax, 4                                      ; Prints out the lowest number 
mov ebx, 1
mov ecx, esp
add [ecx], DWORD 48             
mov edx, 4

exit:                                           ; Program Exit
mov eax, 1
xor ebx, ebx
int 0x80
;Exit

;Function Start                                 ; Function for finding lowest value
outter:                                         ; Compare Main
cmp edi, 96                                     ; 
ja  funct_end                                   ; end the function at edi => 100
mov esi, 0                                      ; initialize esi when this part of loop is run    

inner:
cmp esi, 4                                      ; Compare esi (inner loop index) to 5
ja inner_end                                    ; ends inner loop if > 4 esi's limit 
mov eax, [matrixA + edi + esi * 4]              ; moves element from  matrixA into eax
cmp eax, ecx                                    ; Compare eax element of matrixA to ecx
ja  higher                                      ; jump if value is above ecx value
mov ecx, eax                                    ; sets new lowest number if reached

higher:                                         ; Incrementer
inc esi                                         ; increments sdi
jmp inner

inner_end:                                      ; if outter give a higher value                             
add edi, 20                                     ; row is 5 x 4
jmp outter

funct_end:                                      ; end of function
push ecx
ret
;Function End

非常感谢任何帮助。

【问题讨论】:

    标签: assembly segmentation-fault nasm multidimensional-array


    【解决方案1】:

    这很奇怪:

    push ecx
    ret
    

    我猜你不希望函数返回由ECX 确定的地址。通常,函数的结果在EAX 中返回。所以改成:

    mov eax, ecx
    ret
    

    并在call outter 之后适当地处理它。看看我建议的以下几行:

    call outter
    push eax
    

    当不再需要该值时(例如POP),清除堆栈是个好主意。

    您不会对整数 ascii 转换感到满意,请查看此处或查看 Google 以获得更好的解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-06
      • 2022-01-17
      • 1970-01-01
      • 2020-08-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多