我正在为需要获取数组的作业编写汇编程序,比如说
array: DB 1, 2, 3, 4, 5
我需要遍历它并打印与数组中的数字相对应的星号。所以在上面的例子中,程序应该输出:
* * * * *
* * * *
* * *
* *
*
我的问题是我在代码的某个地方遇到了分段错误,我无法确定在哪里。有人知道这发生在哪里吗?
代码:
%INCLUDE "csci224.inc"
SEGMENT .data
array: DB 1, 2, 3, 4, 5, 4, 3, 2, 1
star: DB "*",0,10
n: DB 0
SEGMENT .text
main:
mov edx, 9 ;length of array
mov ecx, 0 ;loop counter
jmp outerloop ;begin outerloop
outerloop:
mov ah, [array+ecx] ;move (array element + loop counter) to ah
movzx ebx, ah ;zero extend ah to ebx
mov [n], ebx ;copy value of array element to variable
mov eax, ebx
inc ecx
;call WriteInt
dec edx
jnz innerloop ;jump to innerloop
innerloop: ;cycle through '[n]', printing a star each time
mov edi, [n]
mov eax, [star]
call WriteString ;print star
dec edi ;decrement counter
jnz innerloop ;is edi zero? if no, loop again
cmp ecx, byte 0 ;if yes, go back to outerloop
jz outerloop
ret