首页 > 解决方案 > 将索引寄存器分配给通用寄存器

问题描述

我将数组 (arr) 中的每个值除以 2,以检查除法的提示并确定它是奇数还是偶数。问题是每当我将 arr (1) 的第一个值分配给 reg ax 时,存储在 reg ax 中的值是 0201h 而不是 0001h。我真的不明白为什么 0201 被存储而不是 0001。

arr db 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

LEA SI, arr    
mov ax,[SI]    ; the value 0201h is stored instead of 0001h

原始代码

.Model small
.stack 64
.data
arr db 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
countEven db ?
avgResult db ?
.code
;description
Main PROC Far
    mov ax,@data
    mov ds,ax

    LEA SI, arr
    mov countEven, 0
    mov cx,10   ;the size of the array
    mov bh,2    ;the value 2 to be divided by
    mov dx,0    ;the sum container
    loop1:  mov ax,[SI]
            DIV bh
            cmp ah,1    ;if ah = 1, then value within ax was odd (reminder of division by 2 is 1)
            JE skip
            add dx,[SI]
            Inc countEven
            skip: add SI,1
    Loop loop1
    mov ax,dx       ;ax contains the sum
    mov bh,countEven;bx contains the no. of even elements
    div bh          ;ax is the final avg result of the even numbers, where ax=ax/bx
    mov avgResult, al

    MOV AH,4CH
    INT 21H
    
Main ENDP
End Main

标签: assemblyx86-16

解决方案


推荐阅读