无法在程序集中索引数组

问题描述 投票:0回答:1

我一直在尝试学习一些汇编并测试数组,发现当我尝试打印索引点处的值时没有发生任何事情,经过进一步实验后,似乎即使我使用了许多中所示的数组互联网上的例子,它只是不起作用

这是代码:

section .text
    global _start

_start:
    mov eax, num
    mov ebx, [array+8]
    cmp eax, ebx
    jge skip
    call printdigit
skip:
    call printn
    call _exit

printdigit:
    mov eax, 0x30
    add [num], eax
    mov ecx, num
    mov edx, 1 ;length
    mov ebx, 1 ;write to stdout
    mov eax, 4 ;write call number
    int 0x80
    ret

printn:
    mov eax, 0x0A
    push eax
    mov eax, 4
    mov ebx, 1
    mov ecx, esp
    mov edx, 1
    int 0x80
    add esp, 4
    ret

_exit:
    mov eax, 1
    mov ebx, 0
    int 0x80


section .data
    num dw 5
    array dw 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

我用来编译代码的命令

nasm -f elf Bubblesort.asm
ld -m elf_i386 -s -o Bubblesort Bubblesort.o
./Bubblesort

我正在跑步:

ubuntu 22.04.3 桌面 amd64,(在虚拟机上,但我认为应该不重要)

我想要的输出应该是

5

实际产量


我几乎可以肯定这不是计算机问题而是代码问题,但我不确定在哪里

assembly x86 x86-64
1个回答
0
投票

mov eax, num

eax = num 的地址,我的调试器显示值 00402000。

mov ebx, [array+8]

ebx = 00050004。

cmp eax,ebx

eax >= ebx,因此代码跳转到

skip
标签。

Write 函数打印

0x0A
,即
new line
代码。如果您更改为
0x35
您将看到
5

printdigit
代码永远不会被执行。

© www.soinside.com 2019 - 2024. All rights reserved.