Author Topic: Where is the extra byte disappearing to?  (Read 5269 times)

Offline georgelappies

  • Jr. Member
  • *
  • Posts: 12
Where is the extra byte disappearing to?
« on: May 02, 2012, 09:02:04 PM »
Hi all

I am trying to write a DOS program using buffered input. If I make the input buffer 5 bytes long I can only enter 4 bytes. Here is the program:

Code: [Select]
; --- set the starting location in memory as well as the program
; --- to 16 bits
bits 16
org 0x100

; --- jump to main program
jmp main

; --- initialised data
outStr: db "Please enter a number, max of 5 digits long: "
cr_lf: db 0dh, 0ah, '$' ; caraige line feed
in_buf: db 6 ; length of input buffer
len_in: db 0 ; length of input string
inInt: times 6 db ' ' ; reserve and init 6 bytes for input

; --- display routine - asumes value to show is in dx
displayMsg:
mov ah, 09h ; request display msg
int 21h ; display msg
ret ; return from function
; --- buffered input routine
inputInt:
mov ah, 0x0a ; request buffered input
int 21h ; get input
ret
; --- main program
main:
mov dx, outStr ; move address of msg to display to dx
call displayMsg ; display message

mov dx, in_buf ; move address of input buffer into dx
call inputInt ; get buffered input
mov dx, cr_lf ; move carraige return + line feed
call displayMsg ; write cr_lf to screen

int 20h ; terminate program

Does DOS auto-magically convert the buffered input into a c-string such that any input is null terminated and that is where the one byte goes missing?

P.S. Apart from DEBUG.EXE what other debuggers for DOS can one use?
« Last Edit: May 02, 2012, 09:04:11 PM by georgelappies »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Where is the extra byte disappearing to?
« Reply #1 on: May 02, 2012, 11:03:44 PM »
RTFM on that int 21h/0Ah. It requires a "special" buffer - a byte for "max length", and a zero byte to return "length entered", your entered text will begin at buffer + 2. It will not be zero-terminated. It will be CR terminated - you can overwrite the CR with zero, or '$' (more useful), or check the length byte at buffer + 1 and tell int 21h/40h the length.

Code: [Select]
; --- set the starting location in memory as well as the program
; --- to 16 bits
bits 16
org 0x100

; --- jump to main program
jmp main

; --- initialised data
outStr: db "Please enter a number, max of 5 digits long: "
cr_lf: db 0dh, 0ah, '$' ; caraige line feed
in_buf: db 6 ; length of input buffer
len_in: db 0 ; length of input string
inInt: db 6, 0
 times 6 db ' ' ; reserve and init 6 bytes for input

; --- display routine - asumes value to show is in dx
displayMsg:
mov ah, 09h ; request display msg
int 21h ; display msg
ret ; return from function
; --- buffered input routine
inputInt:
mov ah, 0x0a ; request buffered input
int 21h ; get input
ret
; --- main program
main:
mov dx, outStr ; move address of msg to display to dx
call displayMsg ; display message

mov dx, in_buf ; move address of input buffer into dx
call inputInt ; get buffered input
mov dx, cr_lf ; move carraige return + line feed
call displayMsg ; write cr_lf to screen

int 20h ; terminate program

I used to be fond of David Lindauer's GRDB - like DEBUG only smarter...

Best,
Frank