2

I wonder how do I have to pass pointer argument to procedure?

I have to create function who have 2 arguments:

  1. words array
  2. array's size

The function gets array that the array's size and sum the column.

That's what I've coded:

.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

the problem - it's adding to result the offset pointer (here is cs:0000,cs:0001,cs:0002,cs:0003) and not the offset value (here is: 1,2,3,4).

Because of this, result will be 6 and not 10.

could someone help me?

1 Answer 1

2
INC BX

will of course add 1 (byte) to the pointer in BX. If you need to move one WORD, you have to add the size of the word. Say that is 2 bytes, then you need

ADD BX, 2

instead of the INC.

Your other problem is that you don't add the values pointed-to, [BX], but the pointers themselves BX. You might use a spare register, like AX to compute the sum.

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

    RET  4
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.