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

In summary, the conversation is discussing programming in the 8086 CPU and the use of different functions for input and output, such as Func 01H for single character input and Func 0AH for buffered input. The recommended size for the user stack is mentioned and the specific return values for Func 0AH and Func 09H are explained. The conversation ends with a note from Tom regarding the correct references for the functions.
  • #1
user8989898
1
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
  • #2
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:
  • Informative
Likes berkeman

FAQ: X86 NASM reading from the terminal and writing back to the terminal

How do I read a string from the terminal in X86 NASM?

To read a string from the terminal, you typically use the `sys_read` system call. You need to set up the registers with the appropriate values: `eax` with the system call number for `sys_read` (usually 3), `ebx` with the file descriptor for standard input (0), `ecx` with the buffer to store the input, and `edx` with the maximum number of bytes to read.

How do I write a string to the terminal in X86 NASM?

To write a string to the terminal, you use the `sys_write` system call. Set `eax` with the system call number for `sys_write` (usually 4), `ebx` with the file descriptor for standard output (1), `ecx` with the buffer containing the string, and `edx` with the length of the string. Then, execute the `int 0x80` instruction to make the system call.

How can I handle user input of unknown length in X86 NASM?

Handling user input of unknown length typically involves reading a fixed-size buffer in a loop until you encounter a newline character or EOF. You can dynamically allocate memory or use a sufficiently large buffer to ensure you capture the entire input. After reading, you can process the buffer to handle the input appropriately.

What are the common pitfalls when reading from the terminal in X86 NASM?

Common pitfalls include not reserving enough space in the buffer, not properly null-terminating strings, and not handling the return value of `sys_read` correctly. It's important to check the number of bytes actually read and handle cases where the input exceeds the buffer size. Additionally, forgetting to account for the newline character can lead to unexpected behavior.

How do I convert the terminal input to an integer in X86 NASM?

To convert terminal input to an integer, you need to read the input as a string and then parse it. This typically involves iterating over each character, converting it from ASCII to its numeric value, and accumulating the result. You must handle possible issues like non-numeric characters and overflow conditions.

Similar threads

Back
Top