assembly x86 使用堆栈的值作为指针?

标签 assembly x86 x86-16

我想知道如何将指针参数传递给过程?

我必须创建有两个参数的函数:

  1. 单词数组
  2. 数组的大小

该函数获取数组,该数组的大小并对列求和。

这就是我编码的内容:

.MODEL  Small
.STACK  64

; +===============================+
; |             DATA              |
; +===============================+

.DATA 
array1      dw  1,2,3,4
array1size  dw  4
result      dw  ?
address     dw  ?
; print
TMP     dw  0 ; general temporary variable ..
.code

addNumbers proc
;   reset result
    lea di,result
;   use stack
    mov bp,sp
;   get num array
    mov bx,[bp+2]
;   get num of numbers
    mov cx,[bp+4]
; making additiontion
adding:
    add [di],bx
    inc bx; go to the next bx
loop adding
    ret 2
endp

; start
start:
    mov ax,@DATA
    mov ds,ax
; set strings
    push array1size
    push offset array1
    call addNumbers
; print
    mov dx:ax,result
    call printNumber
    mov ah,4ch
    mov al,0
    int 21H
end start

问题 - 它添加了偏移指针(这里是 cs:0000,cs:0001,cs:0002,cs:0003)而不是偏移值(这里是:1,2,3,4 )。

因此,结果将是 6 而不是 10。

有人可以帮助我吗?

最佳答案

INC BX

当然会给BX中的指针加1(字节)。如果需要移动一个单词,则必须加上单词的大小。假设是2个字节,那么你需要

ADD BX, 2

而不是INC

您的另一个问题是您没有添加指向的值[BX],而是添加了指针本身BX。您可以使用备用寄存器(例如 AX)来计算总和。

    MOV  AX,0
adding:
    ADD  AX,[BX]
    ADD  BX,2
    LOOP adding
    MOV  [result],AX

    RET  4

关于assembly x86 使用堆栈的值作为指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10829921/

相关文章:

assembly - 汇编中按值传递和按引用传递

c - Arm Cortex-M4 LDRD 指令导致硬故障

c++ - 使用 EBP 从堆栈中获取参数

c++ - __do_global_dtors_aux 和 __do_global_ctors_aux

x86 - 我的 macbook pro x86 linux 还是 x86_64 darwin?

assembly - 为什么一条指令不能在汇编中包含两个内存引用?

function - as8088中的变量初始化

assembly - 8086锁销和ASM LOCK前缀如何工作

assembly - Intel X86-64 组装教程或书籍

multithreading - 缓存一致性的重点是什么?