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

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
20
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
  • Programming and Computer Science
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
3K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
7
Views
6K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
7
Views
7K
Back
Top