Mic*_*ael 1 assembly initialization dos memory-segmentation x86-16
我已经编写了一个代码,它应该生成某种数字列表,但是即使我确实为它们分配了初始值,我的数据段变量也没有被初始化?
这是我的代码,但数据段只保留垃圾值:
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
Run Code Online (Sandbox Code Playgroud)
你知道如何解决吗?
当 .EXE 程序在 DOS 环境中启动时,DS
段寄存器指向 ProgramSegmentPrefix PSP。这就是我们在包含的屏幕截图中看到的内容。
ASSUME DS:DATA
仅是汇编程序的指示,因此它可以验证数据项的可寻址性。要真正DS
指向您的DATA SEGMENT
,您需要像mov ax, @DATA
mov ds, ax
. 把它放在你的代码开始执行的地方。
start:
mov ax, @DATA
mov ds, ax
;input
mov ah, 1
int 21h
Run Code Online (Sandbox Code Playgroud)
不相关,但是您的递归过程调用将失败,因为您没有BP
在调用之间保留寄存器,或者没有从SP
寄存器重新加载它。