Search code examples
assemblyinitializationdosx86-16memory-segmentation

The data segment is not being initialized even though I did set an initial value to the variables


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?

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

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

Do you know how to solve it?


Solution

  • 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 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
    

    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.