内存中的字符串应该保存在什么地方,以免丢失DS的内容?

Where can I save the string in memory to avoid losing the content of DS?

我有以下代码:

[bits 16]
org  0x100

segment .text
    global start

start:
    lea si,[msg0]
    call print

    call gets

    lea si,[msg1]
    call print

    lea si,[ds:0]
    call print

    ; Termina el programa
    mov ah,00h
    int 21

gets:
    xor bx,bx
    mov ds,bx

gets_start:
    ; Leemos un caracter
    mov ah,00h
    int 16h

    ; Comprobamos si se presiono enter
    cmp ah,C
    je gets_end

    ; provocamos eco
    mov dl,al
    mov ah,02h
    int 21h

    ; Almacenamos el caracter
    mov [ds:bx],al
    inc bx
    jmp gets_start

gets_end:
    ; agregamos 0 al final de la cadena
    xor al,al
    inc bx
    mov [ds:bx],al

    ; Nueva linea
    mov dl,0xA
    mov ah,02h
    int 21h

    ret

print:
    ; Movemos el caracter a dl
    mov dl,[si]

    ; Comprobamos si el caracter es 0
    cmp dl,0
    je print_end

    ; Imprimimos el caracter
    mov ah,02h
    int 21h

    ; Avanzamos al siguiente caracter
    inc si
    jmp print

print_end:
    ; Termina de imprimir
    ret

segment .data
    msg0 db "Ingrese su nombre: ",0
    msg1 db "Hola ",0

但是在 gets 中我使用 DS 寄存器来保存一个字符串并且我丢失了对 DS 寄存器的引用(因此我无法打印 msg1)。我可以在哪里保存字符串?

我是汇编新手,正在学习内存管理。

1) 您可以在堆栈上保存几乎任何 16 位寄存器:

push ds
...
pop ds
ret

请注意,必须按照与存储时相反的顺序读取寄存器。以下示例将交换 axds 的值,因为 ax 的旧值最后存储,因此将首先读取:

push ds
push ax
...
pop ds
pop ax

请注意,在这种情况下,ret(至少在使用近内存模型时)实际上意味着 pop ip。所以你必须读取存储到堆栈的所有值 在执行 ret 指令之前返回寄存器。

2) 您正在将字符串写入地址 DS:BX=0:0.

在实模式下,该地址包含中断table。往里面写数据不是个好办法:字符串够长肯定会死机!

这样做会更有意义:

    ...

gets:
    lea bx, [strgbuf]
gets_start:

    ...

segment .data
    msg0 db "Ingrese su nombre: ",0
    msg1 db "Hola ",0
    ; Space for the string
    strgbuf db "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"