Click here to Skip to main content
15,885,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am trying to learn ASM programming on MS-DOS to get a string from User.
But during the run-time it reboots the OS automatically or hangs or misbehaves while the same program is executed on Windows-XP it does not give any run-time errors.

Could you please help me in this regards as to where am I going wrong.

Below is the environment:
Assembler: Tasm
OS: MS-DOS71, XP

ASM
.Model Small
.Stack 100H .Data
    CR Equ 0DH
    LF Equ 0AH
    NL Equ 00H
    MsgX db 32H Dup (NL)
.Code
Run:
    MOV AX, @Data
    MOV DS, AX
    MOV AX, Offset MsgX
    CALL GetStr
    CALL Exit

    GetChr:
        PUSH BX
        PUSH CX
        PUSH DX
        MOV AH, 01H
        INT 21H
        POP  DX
        POP  CX
        POP  BX
    RET

    GetStr:
        PUSH AX
        PUSH BX
        PUSH CX
        PUSH DX
        MOV BX, AX
        GetStrLoop:
            CALL GetChr
            MOV Byte Ptr [BX], AL
            CMP Byte Ptr [BX], CR
            JE GetStrFree
            INC BX
            JMP GetStrLoop
        GetStrFree:
            MOV Byte Ptr [BX], NL
            POP DX
            POP CX
            POP BX
            POP AX
    RET

    Exit:
        MOV AH, 4CH
        INT 21H
End Run
Posted
Updated 18-Mar-20 1:45am
Comments
[no name] 29-Sep-12 15:39pm    
Well I wanted to mention one of the errors I get during the run-time as below
"cpu error incorrect opcode. killing process"
Kuthuparakkal 29-Sep-12 16:54pm    
try changing this MOV AH, 01H
to: MOV AH, 09H
[no name] 29-Sep-12 17:03pm    
Are you just guessing? 09H function is used to display string to the screen while 01H is used to accept a character from screen.
What difference you think so it really makes if i even change them ?

1 solution

This codepiece reminds me of some good old times... :-)
At first sight your code has no serious errors that should cause a crash. maybe you compiled and/or linked it incorrectly. Your prgram should be compiled into an exe file, not a com file. Check if it has an exe header, open it with a binary viewer and check if the file starts with "MZ" or "ZM". If it doesn't have an exe header then you linked it incorrectly as a com file.

For string input I would use dos function 10 unless your task is write one using character input. The one using character input is useful when you want restrictions on the input (for example allowing only digits or maximize string length).

Some notes about the code:
- You are too paranoid and using too many PUSHs and POPs. Your getchar function could be inlined to your loop.
- You don't check for max string length, this can lead to buffer overrun if the user enters a very long string.
- You don't handle backspace but that would involve much more work.
- You don't specify the program exit code in al when you exit, for example zero: mov ax, 4c00h int 21h
- The correct name for the "newline character" (ascii 10) is Line Feed (aka LF) in DOS, CR is okay. :-)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900