Comp Sci X86 NASM reading from the terminal and writing back to the terminal

AI Thread Summary
The discussion focuses on using NASM assembly language to read from and write to the terminal on an x86 CPU, specifically the 8086 architecture. The code provided demonstrates buffered input and output using DOS interrupts, particularly functions 0AH for reading input and 09H for printing strings. It highlights the importance of correctly managing input buffers and the internal stack when interfacing with DOS. The conversation also clarifies the difference between buffered and unbuffered input methods, suggesting alternatives for specific use cases. Overall, the thread emphasizes proper function usage and stack management in assembly programming for terminal interactions.
user8989898
Messages
1
Reaction score
0
Homework Statement
Please i need to do this in 8086 NASM assembly:
Read lines of text from the terminal. Write to the terminal the first half of the read lines and from each line only the first half of the read bytes.

You count half as a whole number. Half of eg 5 are 2. Then the first half will contain 2 lines or bytes and the second half 3 lines or bytes.

There can be up to 600 lines in the input, each line can be up to 100 bytes long.
Even empty lines must be passed to the output.
On the output, each line must be terminated with CR LF characters, regardless of which line separator it was terminated on the input.
For simplicity, only letters without diacritics, numbers and spaces appear in the loaded data.
Relevant Equations
Need help
Code:
cpu 8086
segment    code
..start    mov ax,data
    mov ds,ax
    mov ax,stack
    mov ss,ax
    mov sp,dno
    mov es, ax

start_2    mov ah, 0x0a
    mov dx, buffer
    int 21h
    cmp al, 0
    je print_lines
    
    inc word [count]
    mov si, buffer
    mov di, line
    mov cx, 100
    rep movsb
    jmp print_lines
    
print_lines mov ax, [count]
    shr ax, 1
    mov [half], ax
    mov bx, [half]
    mov cx, [half]

print_line mov dx, line
    mov ah, 9
    int 21h

    mov si, line
    mov di, line_end
    mov cx, 2
    rep movsb

    mov ax, 100
    shr ax, 1
    mov [half_byte], ax
    add si, [half_byte]
    mov cx, 100
    rep movsb
    dec bx
    jz end
    jmp print_line

empty_line mov ah, 9
    mov dx, empty_string
    int 21h
    jmp print_lineend    hlt

segment    data
buffer    db 100
half    dw 0
count    dw 0
half_byte dw 0
line_end db 0Ah, 0Dh
line    db 100
empty_string db 0Ah, 0Dh

segment    stack
    resb 100
dno:    db ?
 
Last edited by a moderator:
Physics news on Phys.org
It looks like your code is trying to operate on a single character at a time. The single character input is Func 01H, single character output is Func 02H; however you are using Buffered input and output, Func 0AH and 09H, which operate on a line at a time.

Note that Func 01H echos all Standard Input characters to the Standard Output.
If this is not desired, Func 06H Direct Console I O can be used.

Following from IBM Dos Technical Reference version 3.30.

DOS Internal Stack

When DOS takes control, it switches to an internal stack. User registers are preserved unless information is passed back to the requester as indicated in the specific requests. The user stack needs to be sufficient to accommodate the interrupt system. It is recommended that the user stack be 200H in addition to the user needs.

Int 21H, Func 0AH Buffered Keyboard Input
On Return, Register Contents NONE​
The first byte of the input buffer specifies the number of characters the buffer can hold. This value can not be zero.​
The second byte of the buffer is set to the number of characters received, excluding the carriage return, which is always the last character.​

Int 21H, Func 09H Print String
On Return, Register Contents NONE​
The character string in memory must be terminated by a $ (24H). each character in the stream is output to the standard output device in the same form as call to Func 02H.​

(edit: Func references were shown as Int. They are all Functions of Int 21H)
(/edit)

Cheers,
Tom
 
Last edited:
Back
Top