Pregunta

I'm a total beginner to 8086 assembly language. I want to try some simple stuff first. How do write a program, to enter a number, say x, store it in memory and then later on load it to a register, and display it?

I did something like this :

.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

What's the mistake? Please help me! Thanks!

¿Fue útil?

Solución

There is two mistakes in above code

  1. The $ sign have to be in quotes otherwise error can be generate.

  2. After taking the input we have to mov the value of al in dl, to print the entered value on screen.

Otros consejos

There are some mistakes in the code. For to enter a string with using the DOS function 0Ah (BUFFERED INPUT) we have to use an input buffer. The format of this DOS input buffer is shown (in Table 01344) below.

Ralph Browns x86/MSDOS Interrupt List:
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

For to display the ASCII string (from the user input) using DOS function 09h we have to get the offset address of the buffer+2 into DX and additional we have to place a "$" after the string before calling the function. The address for to store the "$" after the string can be calculate with adding the number of chars from last input.

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

This example above make more sense if the number of bytes for input is greater than 1 and so it is possible that the user do not fill the maximum numbers of ASCII into the buffer, so we do not know the end of the string and so we have to get the number of ASCII from the last input. (But allways we have to make sure that the dimension of the buffer is large enough.)

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

Dirk

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

This is a example of stack in 8086 compiler.Thanks

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top