Haseeb Ur Rehman
Haseeb Ur Rehman

Reputation: 45

Garbage output from an x86 assembly language program

I am having a problem with a small assembly language program.

This program takes the input and converts it into uppercase or lowercase. It is converting properly, but is giving garbage output and I don't know why.

.model small
.stack 64

.data
data1 Db 15 dup(?),'$'
data2 Db 15 dup(?),'$'

.code
start:
    MOV     AX, @data
    MOV     DS, AX
    MOV     SI, OFFSET data1
    MOV     BL, 0Dh
lop:
    MOV     AH, 01h
    INT     21h
    CMP     AL, BL
    JE      con
    MOV     [SI], AL
    INC     SI
    LOOP    lop
con:
    MOV     SI, OFFSET data1
    MOV     DI, OFFSET data2
conv:
    MOV     AL, 60h
    CMP     [SI], AL
    JBE     lo
    CMP     [SI], AL
    JAE     co
    CMP     [SI], BL
    JE      quit
    JMP     quit
co:
    MOV     AL, [SI]
    SUB     AL, 20h
    MOV     [DI], AL
    INC     SI
    INC     DI
    LOOP    conv
lo:
    MOV     AX, [SI]
    ADD     AX, 20h
    MOV     [DI], AX
    INC     SI
    INC     DI
    LOOP    conv

quit:
    MOV     DX, OFFSET data2
    MOV     AH, 09h
    INT     21h
    MOV     AH, 4Ch
    INT     21h

END start

Upvotes: 0

Views: 1432

Answers (2)

Sep Roland
Sep Roland

Reputation: 39166

The fact that you are quiting this program with the DOS Terminate function somehow makes it unbelievable that CX would be initialized beforehand!
You definitely need to initialize CX=15 at the start of your 'lop' loop.

If while inputting you press ENTER then CX will have a residual value. Good but still faulty. If not, CX will be truly disastrously 0 at the start of your 'con:' program part.

Your conversion is based on a single comparison with ASCII 60h. This will inevitably result in garbage when presented with non alphabetic characters.

The second time you write cmp [si],al is superfluous.
The following jae co only needs ja co.
The trio instructions that follow are never executed.

The capitalization part 'co:' should certainly never fall through in the next program part which mistakenly uses AX instead of AL.

You would better initialize buffer 'data2' with 16 $ characters because now there's no telling what DOS will display!

Upvotes: 1

Brian Knoblauch
Brian Knoblauch

Reputation: 21359

I haven't sat down and tried the program, but I've found that sometimes the assembler gets confused when using OFFSET and the substituting with LEA will magically solve those problems. If that doesn't work, then you've got a logic error that I'm just not seeing.

Upvotes: 0

Related Questions