Click here to Skip to main content
11,577,643 members (22,497 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASM
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

.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 29-Sep-12 9:07am
Comments
Zerr0cool at 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 at 29-Sep-12 16:54pm
   
try changing this MOV AH, 01H
to: MOV AH, 09H
Zerr0cool at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

This codepiece reminds me of some good old times... Smile | :)
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. Smile | :)
  Permalink  
v2

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

  Print Answers RSS
0 DamithSL 180
1 OriginalGriff 115
2 Richard MacCutchan 100
3 Peter Leow 100
4 Abhinav S 100
0 OriginalGriff 820
1 Sergey Alexandrovich Kryukov 681
2 Abhinav S 528
3 F-ES Sitecore 420
4 Suvendu Shekhar Giri 375


Advertise | Privacy | Mobile
Web03 | 2.8.150603.1 | Last Updated 29 Sep 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100