我在哪里可以将字符串保存在内存中,以避免丢失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,$1C
    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

在实模式下,此地址包含中断表。在那里写入数据不是一个好主意:如果字符串足够长,计算机肯定会崩溃!

这样做会更有意义:

    ...
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"

最新更新