Assembly language: Why wouldnt my code work the way i want it to?

Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
7 replies · 8K views
kloong
Messages
35
Reaction score
0
this is my code to convert lower case strings to upper case.
i want to ask the user whether to continue or not. but no matter what i type, the prog keeps stoping. try to copy paste this code to ur emulator and u'll know what I am saying.

also, how should i replace the include 'emu8086.inc' with??
.
.MODEL SMALL

.STACK 100H

.DATA

MSG1 DB 0DH,0AH,"ENTER A LOWER CASE LETTER: $"
MSG2 DB 0DH,0AH, "IN UPPER CASE IT IS:"
CHAR DB ?, "$"

.CODE
MAIN PROC
include 'emu8086.inc'

MOV AX, @DATA
MOV DS, AX

label3:
LEA DX, MSG1


MOV AH, 9
INT 21H

MOV AH, 1
INT 21H

CMP AL, 'z'
JG label1

CMP AL, 'a'
JL label2

SUB AL, 20H

MOV CHAR, AL

LEA DX, MSG2

MOV AH,9
INT 21H

JMP label4


label1:
PRINT ' Please enter a lower case alphabet for conversion!'
jmp label3

label2:
PRINT ' Please enter a lower case alphabet for conversion!'
jmp label3

label4:
PRINT ' Do you want to continue?<y/n>'
MOV AH, 1
INT 21H

CMP AL, 'n'
int 20h

CMP AL, 'y'
jmp label3

exit:
MAIN ENDP
END MAIN
 
Physics news on Phys.org
The problem is here:

CMP AL, 'n'
int 20h

You do a compare, but then exit (int 20h) no matter what. You need a conditional branch after a compare.


I don't know what emu8086.inc does, are you running on a non-intel cpu?

If you're using MASM, (Microsoft's assembler), it defaults to 8086 unless you specify a cpu, such as .186 or .686.
 
Last edited:
i see. so how i should i do a conditional branch? can you show it to me?and i am using the 8086 emulator. if i take that out, there will be a warning message that says my parameter is wrong. i copied that line from the example given. er, how should i fix that?
 
Well, CMP does a logical subtraction between source and destination, so you should use:

jz: jump if zero
je: jump if equal

jne: jump if not equal
jnz: jump if not zero

jg: jump if greater than
jge: jump if greater than or equal to

jl: jump if less than
jle: jump if less than or equal to

I would recommend using these, and not the ones for unsigned arithmetic, since the logical subtraction is involved (suggestion).

For example:

cmp al, 'n'
je equalToNBranch
jmp notEqualToNBranch

equalToNBranch: ; code for the case al == 'n'
jmp done;

notEqualToNBranch: ; code for the case al =/= 'n'
jmp done;

done: ; further code...

Not the most efficient usage, but hopefully clear enough to give you the idea.
 
thanks, it works and i also mod-ed it to work so that when the user enter something other than y/n, it will keep asking the user to enter y/n again. but how do i make them go to a new line everytime?

label4:
PRINT ' Do you want to continue?<y/n>'
MOV AH, 1
INT 21H

CMP AL, 'n'
JE label6
JNE label7

label7:
CMP AL, 'y'
JE label3
JNE label4

label6:
int 20h
 
i know 0dh and 0ah. but that can't work.

but the more important question i have now is, how do i 'declare' or 'pass' the parameter?

in my code, i used something like: include emu8086:

which, i copied from the example. if i don't do that, the "PRINT" wouldn't work. i know it's bad programming but i didnt know what to do. so what should i do to replace that line (include emu8086)?

anyway, this is my code:

.
.MODEL SMALL

.STACK 100H

.DATA

MSG1 DB 0DH,0AH,"ENTER A LOWER CASE LETTER: $"
MSG2 DB 0DH,0AH, "IN UPPER CASE IT IS:"
CHAR DB ?, "$"

.CODE
MAIN PROC
include 'emu8086.inc'

MOV AX, @DATA
MOV DS, AX

label3:
LEA DX, MSG1MOV AH, 9
INT 21H

MOV AH, 1
INT 21H

CMP AL, 'z'
JG label1

CMP AL, 'a'
JL label2

SUB AL, 20H

MOV CHAR, AL

LEA DX, MSG2

MOV AH,9
INT 21H

JMP label4label1:
PRINT ' Please enter a lower case alphabet for conversion!'
jmp label3

label2:
PRINT ' Please enter a lower case alphabet for conversion!'
jmp label3

label4:
PRINT ' Do you want to continue?<y/n>'
MOV AH, 1
INT 21H

CMP AL, 'n'
JE label6
JNE label7

label7:
CMP AL, 'y'
JE label3
JNE label4

label6:
int 20h

exit:
MAIN ENDP
END MAIN
 
sorry again. it's all good now. thank you, Jeff and csprof2000! have a nice day! =)