即使我确实为变量设置了初始值

即使我确实为变量设置了初始值

本文介绍了即使我确实为变量设置了初始值,数据段也没有被初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个代码,它应该生成某种数字列表,但是即使我为它们分配了初始值,我的数据段变量也没有被初始化?

I have written a code that is supposed to make some sort of a list of numbers, but my data segment variables are not being initialized even though I did assign them an initial value?

这是我运行 DS:0000 时的样子:

This is how DS:0000 looks when I run it:

这是我的代码,但数据段只保留垃圾值:

This is my code, but the data segment just keeps the trash values:

MODEL small
STACK 100h

DATA SEGMENT
    size1 dw 0000h
    arr dw 20 dup(0000h)
DATA ENDS

CODE SEGMENT
ASSUME CS:CODE, DS:DATA

sidra_rekursivit proc
    mov bp, sp
    xor ax, ax
    mov ax, [bp+2]
    ; tnai azira
    cmp ax, 1
    je azira

    ; tempo
    mov cx, ax ; save ax
    shr ax, 1
    jnc zugi ; if zugi


izugi:  ; else
    mov ax, cx
    ;multiply by 3
    shl ax, 1
    add ax, cx
    ;end multiply
    ; add 1
    inc ax

    push ax
    call sidra_rekursivit
    jmp azira

zugi:
    push ax
    call sidra_rekursivit

azira:
    ; put the numbers in arr
    mov bx, [size1] ; arr size
    xor dx, dx ; clear dx
    mov dx, [bp+2] ; take the number from the stack
    mov word ptr arr[bx], dx ; put the number in the arr
    inc size1 ; increase the array posiotion
    ret 2
sidra_rekursivit endp

start:
;input
    mov ah, 1
    int 21h

    mov ah, 0
    sub al, 48
    mov dh, 10d
    mul dh
    mov dl, al

    mov ah, 1
    int 21h

    mov ah, 0
    sub al, 48
    add dl, al

; function call: stack push
    ; push input
    xor dh, dh
    push dx
    call sidra_rekursivit

exit:
    mov ax, 4c00h
    int 21h
CODE ENDS
END start

你知道怎么解决吗?

推荐答案

当 .EXE 程序在 DOS 环境中启动时,DS 段寄存器指向 ProgramSegmentPrefix PSP.这就是我们在附带的屏幕截图中看到的内容.

When an .EXE program starts in the DOS environment, the DS segment register points at the ProgramSegmentPrefix PSP. That's what we see in the included screenshot.

ASSUME DS:DATA 只是汇编程序的指示,因此它可以验证数据项的可寻址性.要真正使 DS 指向您的 DATA SEGMENT,您需要像 mov ax, @DATA mov ds, ax 这样的代码>.把它放在你的代码开始执行的地方.

ASSUME DS:DATA is merily an indication for the assembler so it can verify the addressability of data items. To actually make DS point to your DATA SEGMENT, you need code like mov ax, @DATA mov ds, ax. Put it where your code begins its execution.

start:
  mov ax, @DATA
  mov ds, ax
  ;input
  mov ah, 1
  int 21h

不相关,但您的递归过程调用将失败,因为您没有在调用之间保留 BP 寄存器,或者您没有从 SP 重新加载它注册.

Not related, but your recursive procedure call will fail because either you don't preserve the BP register between calls, or you don't reload it from the SP register.

这篇关于即使我确实为变量设置了初始值,数据段也没有被初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 07:30