使用8086汇编语言从内存中存储和检索数字

时间:2014-04-22 08:01:30

标签: assembly x86-16

我是8086汇编语言的初学者。我想先尝试一些简单的东西。如何编写程序,输入数字,说 x ,将其存储在内存中,然后再将其加载到寄存器中,然后显示它?

我做了类似的事情:

.MODEL SMALL

.DATA
    NL2     DB      0AH,0DH,'Enter a number:','$'
.CODE
MAIN PROC
    MOV si, 100d
    LEA DX,NL2 ; 
    MOV AH,09H  ; 
    INT 21H
    MOV AH,0AH  ; Read into buffer
    MOV [si],0AH ; Store in memory
    MOV BX,[si] ; load from memory to bx
    MOV BX, 09H ; display it
    INT 21H
   .EXIT

    MAIN    ENDP
            END     MAIN

错误是什么?请帮我!谢谢!

4 个答案:

答案 0 :(得分:0)

代码中存在一些错误。要使用DOS函数0Ah(BUFFERED INPUT)输入字符串,我们必须使用输入缓冲区。此DOS输入缓冲区的格式如下所示(表01344)。

Ralph Browns x86 / MSDOS中断列表:
http://www.pobox.com/~ralf/files.html
ftp://ftp.cs.cmu.edu/afs/cs.cmu.edu/user/ralf/pub/

RBIL->inter61b.zip->INTERRUP.F
--------D-2109-------------------------------
INT 21 - DOS 1+ - WRITE STRING TO STANDARD OUTPUT
        AH = 09h
    DS:DX -> '$'-terminated string
Return: AL = 24h (the '$' terminating the string, despite official docs which
        state that nothing is returned) (at least DOS 2.1-7.0 and NWDOS)
Notes:  ^C/^Break are checked, and INT 23 is called if either pressed
    standard output is always the screen under DOS 1.x, but may be
      redirected under DOS 2+
    under the FlashTek X-32 DOS extender, the pointer is in DS:EDX
SeeAlso: AH=02h,AH=06h"OUTPUT"
--------D-210A-------------------------------
INT 21 - DOS 1+ - BUFFERED INPUT
    AH = 0Ah
    DS:DX -> buffer (see #01344)
Return: buffer filled with user input
Notes:  ^C/^Break are checked, and INT 23 is called if either detected
    reads from standard input, which may be redirected under DOS 2+
    if the maximum buffer size (see #01344) is set to 00h, this call returns
      immediately without reading any input
SeeAlso: AH=0Ch,INT 2F/AX=4810h

Format of DOS input buffer:
Offset  Size    Description (Table 01344)
 00h    BYTE    maximum characters buffer can hold
 01h    BYTE    (call) number of chars from last input which may be recalled
        (ret) number of characters actually read, excluding CR
 02h  N BYTEs   actual characters read, including the final carriage ret

为了使用DOS函数09h显示ASCII字符串(来自用户输入),我们必须将缓冲区+ 2的偏移地址输入DX,另外我们必须在调用函数之前在字符串后面放置一个“$” 。可以通过添加上次输入的字符数来计算在字符串后存储“$”的地址。

BUFFER DB 1, ?, " ", 0Dh, "$" ; Input buffer (only for one ASCII)

mov bh, 0                     ; clear high byte of BX
lea si, BUFFER+1              ; get the offset address+1 of the buffer
mov bl, [si]                  ; get the number of byte from the last input
mov BYTE PTR[si+bx+1], "$"    ; store "$" after the end of the string+CR

如果输入的字节数大于1,则上述示例更有意义,因此用户可能没有将最大数量的ASCII填充到缓冲区中,因此我们不知道字符串的结尾所以我们必须从最后一个输入中获取ASCII的数量。 (但总而言之,我们必须确保缓冲区的尺寸足够大。)

lea dx, BUFFER+2
mov ah, 9
int 21h

德克

答案 1 :(得分:0)

.model small
.stack 32h
.data 
Message db " enter a no:" ,'$'
.code
Mov ax,@data
Mov ds,ax
Mov dx,offset message; display message
Mov ah,01h; enter no
Int 21h
Mov dl,al
Mov ah,02h; print no on screen
Int 21h
Mov ah,4ch
Int 21h
End

答案 2 :(得分:0)

上面的代码有两个错误

  1. $符号必须在引号中,否则可能会生成错误。

  2. 在完成输入后,我们必须mov al dl中{{1}}的值,才能在屏幕上打印输入的值。

答案 3 :(得分:-1)

; multi-segment executable file template.

data segment
    ; add your data here!
    pkey db "press any key...$"
ends

stack segment
    dw   128  dup(0)
ends

code segment
start:
; set segment registers:
    mov ax, data
    mov ds, ax
    mov es, ax

    ; add your code here
    mov cx,4
    input:
    mov ah,1
    int 21h
    push ax
    loop input 

    mov dx,13d
    mov ah,2
    int 21h

    mov dx,10d
    mov ah,2
    int 21h

    mov cx,4  

    output:
    pop bx
    mov dl,bl
    mov ah,2
    int 21h
    loop output

    exit:        
    lea dx, pkey
    mov ah, 9
    int 21h        ; output string at ds:dx

    ; wait for any key....    
    mov ah, 1
    int 21h

    mov ax, 4c00h ; exit to operating system.
    int 21h    
ends

end start ; set entry point and stop the assembler.

这是8086编译器中的堆栈示例。谢谢