简体   繁体   English

汇编代码不能正确使用堆栈

[英]Assembly Code doesn't Work with Stack Correctly

I'm practicing Assembly and I encountered a problem with it.我正在练习组装,但遇到了问题。 Here is my code (Assembly 8086):这是我的代码(程序集 8086):

org 100h

.model small
.stack 100h

.data
    arr db 2, 2, 3, 4, 5
    len equ $-arr
    sum db 0 
    
.code

jmp _start

Sum1 proc
    push bp
    mov bp, sp
    
    mov bx, [bp+6]    
    xor ax, ax         
    
    mov cx, len        
    cmp cx, 0          
    je _end
    
LoopSum:
    mov di, cx         
    add al, [bx+di-1]  
    loop LoopSum

_end:
    mov [bp+4], al     
    pop bp
    ret
Sum1 endp

_start proc
    mov ax, @data
    mov ds, ax
    
    push offset arr
    push offset sum
    call Sum1
    
    mov ax, 4c00h
    int 21h
    ret
_start endp
end

My problem is that the variable sum (represented by bp+4 ) has a wrong value (0 instead of 10 as expected).我的问题是变量sum (由bp+4表示)有一个错误的值(0 而不是 10 正如预期的那样)。 Did I miss something?ㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤ我错过了什么吗?ㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤ

  1. use [bp+4] (instead of [bp+8] )使用[bp+4] (而不是[bp+8]

  2. add another register so you will be able to access it like this: [[bp+4]] (but that's not actually valid syntax; x86 doesn't have double-indirection in one addressing mode. That's why you need another register to load the pointer into.)添加另一个寄存器,这样您就可以像这样访问它: [[bp+4]] (但这实际上不是有效的语法;x86 在一种寻址模式下没有双重间接。这就是为什么您需要另一个寄存器来加载指针。)

    You should change your _end block to this one:你应该把你的_end块改成这个:

_end: 
    mov si, [bp+4]
    mov [si], al     
    pop bp
    ret

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM