Thread Closed

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

 
Share Thread
Feb21-09, 08:33 AM   #1
 

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


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 im 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
PhysOrg.com science news on PhysOrg.com

>> New language discovery reveals linguistic insights
>> US official: Solar plane to help ground energy use (Update)
>> Four microphones, computer algorithm enough to produce 3-D model of simple, convex room
Feb21-09, 07:54 PM   #2
 
Recognitions:
Homework Helper Homework Help
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.
Feb22-09, 12:16 AM   #3
 
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?
Feb22-09, 01:45 AM   #4
 

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


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.
Feb22-09, 02:00 AM   #5
 
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
Feb22-09, 09:24 PM   #6
 
Recognitions:
Homework Helper Homework Help
You left out the 0dh and 0ah in the print statements. Try 0dh, 0ah, ' ...' to see if that's allowed.
Feb22-09, 09:38 PM   #7
 
i know 0dh and 0ah. but that cant 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 dont do that, the "PRINT" wouldnt 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, 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'
JE label6
JNE label7

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

label6:
int 20h

exit:
MAIN ENDP
END MAIN
Feb23-09, 04:50 AM   #8
 
sorry again. it's all good now. thank you, Jeff and csprof2000! have a nice day! =)
Thread Closed

Tags
assembly language

Similar discussions for: Assembly language: Why wouldnt my code work the way i want it to?
Thread Forum Replies
Assembly Language Electrical Engineering 10
Assembly Language Engineering, Comp Sci, & Technology Homework 0
Assembly language Electrical Engineering 5
Assembly code Programming & Comp Sci 4
68000 Assembly code Introductory Physics Homework 0