code to take input string
assemble using nasm on windows machine: nasm file.asm -o file.com
;read the string
mov ah,0x0A ;read
mov dx,buffer ;pointer to buffer
int 0x21
;newline
mov ah,2
mov dl,10
int 0x21
mov dl,13
int 0x21
;put $ sign at end of string
mov bx,buffer+1
mov dx,buffer+2
add dl,byte[bx]
mov bx,dx
mov byte[bx],'$'
;output
mov dx,buffer+2
mov ah,9
int 0x21
;exit
mov ah,0x4c
int 0x21
;buffer
buffer:
db 255 ;len of buffer
db 0 ;num of char read
db 255 ;actual string
;############################
It is working file for string up to 2 char in length and after that it is giving out garbage.
Here's how it should be fixed. See the lines with ;;
.
bits 16 ;; you have to tell NASM to generate 16-bit code
org 0x100 ;; you have to start generating addresses at 0x100 for .COM programs
;read the string
mov ah,0x0A ;read
mov dx,buffer ;pointer to buffer
int 0x21
;newline
mov ah,2
mov dl,10
int 0x21
mov dl,13
int 0x21
;put $ sign at end of string
mov bx,buffer+1
mov dx,buffer+2
add dl,byte[bx]
adc dh, 0 ;; we're doing 16-bit+8-bit=16-bit addition
mov bx,dx
mov byte[bx],'$'
;output
mov dx,buffer+2
mov ah,9
int 0x21
;exit
mov ah,0x4c
int 0x21
;buffer
buffer:
db 254; 255 ;len of buffer ;; my doc says the range is 1 to 254
; db 0 ;num of char read ;; you only need to reserve memory
; db 255 ;actual string ;; you only need to reserve memory
resb 255 ;; reserve memory (1 for length, 254 for text)
resb 1 ;; reserve memory for "$"
If the buffer is the last thing in your .COM program, you can actually remove all lines after db 254
.