Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops 在部件中执行嵌套循环的分段错误_Loops_Assembly_X86_Nasm - Fatal编程技术网

Loops 在部件中执行嵌套循环的分段错误

Loops 在部件中执行嵌套循环的分段错误,loops,assembly,x86,nasm,Loops,Assembly,X86,Nasm,我正在为一个任务编写一个汇编程序,比如说,我需要一个数组 array: DB 1, 2, 3, 4, 5 我需要循环遍历它并打印一个与数组中的数字对应的星。因此,在上面的示例中,程序应输出: * * * * * * * * * * * * * * * 我的问题是,我在代码中的某个地方遇到了分段错误,但我无法准确指出错误所在。有人知道这发生在哪里吗 代码: 首先,最后的ret不是结束程序的正确方法!在Windows中,如果要链接到C库,可以使用Exi

我正在为一个任务编写一个汇编程序,比如说,我需要一个数组

array:  DB  1, 2, 3, 4, 5
我需要循环遍历它并打印一个与数组中的数字对应的星。因此,在上面的示例中,程序应输出:

* * * * *
  * * * *
    * * *
      * *
        *
我的问题是,我在代码中的某个地方遇到了分段错误,但我无法准确指出错误所在。有人知道这发生在哪里吗

代码:


首先,最后的
ret
不是结束程序的正确方法!在Windows中,如果要链接到C库,可以使用
ExitProcess
或调用
exit
。在Linux中,如果要链接到C库,可以使用
int80h-sys\u exit
syscall-60
exit

使用嵌套循环时,首先让外部循环工作;打印出数组中的每个值。一旦你有工作,添加你的“内部”循环打印的星星

在下面的代码中,我使用了
esi
ebx
,因为它们必须由被调用方保存,所以我们不必担心保存这些值

extern printf, exit
global main

SEGMENT     .data
array:      DB      1, 2, 3, 4, 5, 4, 3, 2, 1
array_len   equ     $ - array
star:       DB      "*", 0

SEGMENT     .text
main:
    mov     esi, 0                  ;loop counter

.ArrayLoop:
    ;~ Get array value to use as PrintStars loop counter, esi is index into array
    movzx   ebx, byte [array + esi]
    ;~ increase ArrayLoop "Outer Loop" counter
    inc     esi
    cmp     esi, array_len
    ;~ if esi > array_len, we are done
    jg      .Done   

.PrintStars:
    ;~ print stars here

    ;~ decrease "Inner Loop" counter
    dec     ebx
    ;~ If ebx != 0, continue "Inner Loop"
    jnz     .PrintStars

    ;~ ebx == 0, print new line char and continue "Outer Loop"    
    ;~ print newline char here
    jmp     .ArrayLoop

.Done:
    call    exit
它会将星星打印为: 根据源代码中的数组


如果你想制作一个金字塔,请先阅读,你最后的
ret
不是结束程序的正确方法!在Windows中,如果要链接到C库,可以使用
ExitProcess
或调用
exit
。在Linux中,如果要链接到C库,可以使用
int80h-sys\u exit
syscall-60
exit

使用嵌套循环时,首先让外部循环工作;打印出数组中的每个值。一旦你有工作,添加你的“内部”循环打印的星星

在下面的代码中,我使用了
esi
ebx
,因为它们必须由被调用方保存,所以我们不必担心保存这些值

extern printf, exit
global main

SEGMENT     .data
array:      DB      1, 2, 3, 4, 5, 4, 3, 2, 1
array_len   equ     $ - array
star:       DB      "*", 0

SEGMENT     .text
main:
    mov     esi, 0                  ;loop counter

.ArrayLoop:
    ;~ Get array value to use as PrintStars loop counter, esi is index into array
    movzx   ebx, byte [array + esi]
    ;~ increase ArrayLoop "Outer Loop" counter
    inc     esi
    cmp     esi, array_len
    ;~ if esi > array_len, we are done
    jg      .Done   

.PrintStars:
    ;~ print stars here

    ;~ decrease "Inner Loop" counter
    dec     ebx
    ;~ If ebx != 0, continue "Inner Loop"
    jnz     .PrintStars

    ;~ ebx == 0, print new line char and continue "Outer Loop"    
    ;~ print newline char here
    jmp     .ArrayLoop

.Done:
    call    exit
它会将星星打印为: 根据源代码中的数组


如果要制作金字塔,请阅读

,您可以使用调试器找出分段错误发生的位置。你知道怎么做吗?我建议使用gdb。
n
只是一个字节,您正在将一个dword移入移出它
mov-eax,[star]
(“[contents]”)几乎肯定应该是
mov-eax,star
(地址)。您可能需要在
main
周围保存和恢复
ebx
esi
edi
。您可以使用调试器找出分段故障发生的位置。你知道怎么做吗?我建议使用gdb。
n
只是一个字节,您正在将一个dword移入移出它
mov-eax,[star]
(“[contents]”)几乎肯定应该是
mov-eax,star
(地址)。您可能需要在
main
周围保存和恢复
ebx
esi
edi