【问题标题】:Array base index addressing in NASMNASM 中的数组基索引寻址
【发布时间】:2013-11-25 09:21:46
【问题描述】:

我正在尝试遍历定义的“2d”数组并找到最小值。

我在尝试访问矩阵内的值时出错。 请注意,我已尝试替换:

mov ecx, matrix[edi + esi *2]

mov ecx, [matrix + edi + esi * 2]

它没有帮助

;-----------------------------------------------
;SECTION .DATA
;Instantiated variables/Constants
;-----------------------------------------------
section .data

result:     db "The smallest number is: " , 0x0a
result_len:     equ $-result

nl:     db "   ", 0x0a
nl_len  equ $-nl

matrix: dw  25, 24, 23, 22, 21
        dw  20, 19, 18, 17, 16 
        dw  15, 14, 13, 12, 11 
        dw  10,  9,  8,  7,  6
        dw   5,  4,  3,  2,  1


;-----------------------------------------------
;SECTION .BSS
;Non initialized variables
;-----------------------------------------------
section .bss


;-----------------------------------------------
;SECTION .TEXT
;Code
;-----------------------------------------------
section .text
global _start 

_start: 
    ;variable declaration
    mov edi, 0
    mov esi, 0
    mov ecx, 9

outerLoop:
    cmp edi, 50                  ;each element is 2 bytes (2 ascii characters)
    jg  endloop                  ;we need 50 because it's 5 elements per row
    mov esi, 0                   ;and 5 rows
innerLoop:
    cmp esi, 5                   ;Compare esi(inner loop index) to 5
    jge innerEnd                 ;jump if it reached the end of the row
    mov eax, matrix[edi + esi*2]
    cmp [eax], ecx
    jg  biggerThan
    mov ecx, [eax]
biggerThan:
    inc esi
    jmp innerLoop
innerEnd:
    add edi, 10                  ;row has been complete, go to next
    jmp outerLoop

endloop:
    push    ecx

    mov eax, 4
    mov ebx, 1
    mov ecx, result
    mov edx, result_len
    int 0x80

    mov eax, 4
    mov ebx, 1
    mov ecx, esp
    add [ecx], DWORD 48
    mov edx, 2
    int 0x80

    ; display new line
    mov eax, 4
    mov ebx, 1
    mov ecx, nl
    mov edx, nl_len
    int 0x80

 exit:
    mov eax, 1          ;eax contains 1 so quit
    mov ebx, 0
    int 0x80

如果有人能解释为什么这行

mov eax, matrix[edi + esi*2]

它不工作或我应该怎么做 遍历数组并找到最小的,我将不胜感激。

【问题讨论】:

    标签: arrays assembly x86 nasm


    【解决方案1】:
    mov eax, matrix[edi + esi*2]
    cmp [eax], ecx
    jg  biggerThan
    mov ecx, [eax]
    

    Nasm 想要方括号内的所有内存引用,所以mov eax, [matrix + edi + esi * 2] 应该是正确的。但是您正在将 4 个字节从内存(您的两个值)移动到 eax。你只需要两个字节。但是cmp [eax], ecx 尝试将ecxeax 地址处的内存进行比较,这几乎肯定不是有效内存。你可能想要更像...

    mov ax, [matrix + edi + esi*2]
    cmp ax, cx
    jg  biggerThan
    mov cx, ax
    

    您的显示例程仅适用于单个数字。由于矩阵中的最小值只有一个数字,因此这可能不会造成问题。

    查看jgja 之间的区别。您的值都是正数,因此您可能意味着将它们视为无符号 - ja 是正确的。如果您建议允许负数,jg 是正确的。如果您的值是正数且小于 2G,则两者都可以,但您不妨了解其中的区别。

    你在正确的轨道上!

    【讨论】:

      【解决方案2】:

      求最小值是一个基本算法,只要把数组的初值,放入一个“最小变量”中,然后比较下一个,如果下一个较小,则将其保存到“最小变量”中不是,然后跳过它并进入数组中的下一个项目。

      对于数组中的项目,您可能只是没有使用正确的内存地址。啊啊啊汇编编程之美

      【讨论】:

        猜你喜欢
        • 2011-12-02
        • 1970-01-01
        • 2014-02-09
        • 1970-01-01
        • 1970-01-01
        • 2021-08-24
        • 1970-01-01
        • 1970-01-01
        • 2013-08-17
        相关资源
        最近更新 更多