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

In summary, the code is designed to convert lower case letters to upper case, but it keeps stopping. There is a compare error in the code, and you need to use a conditional branch to exit the program no matter what the user enters.
  • #1
kloong
36
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
 
Technology news on Phys.org
  • #2
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:
  • #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?
 
  • #4
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.
 
  • #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
 
  • #6
You left out the 0dh and 0ah in the print statements. Try 0dh, 0ah, ' ...' to see if that's allowed.
 
  • #7
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
 
  • #8
sorry again. it's all good now. thank you, Jeff and csprof2000! have a nice day! =)
 

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

1. Why is my code not producing the desired output?

There could be multiple reasons why your code is not working as expected. It could be due to a logical error in your code, incorrect syntax, or a problem with your computer's hardware or operating system. Carefully review your code and try to identify any errors or bugs. If you are still unable to find the problem, try seeking help from other programmers or consulting online resources.

2. How can I debug my assembly language code?

Debugging is an essential part of the programming process. To debug your assembly language code, you can use a debugger tool that allows you to step through your code and identify any errors or bugs. You can also try using print statements or logging to track the values of your variables and identify any issues.

3. Can my code be affected by the computer's hardware or operating system?

Yes, assembly language is hardware-specific, meaning that it is designed to work with a specific computer architecture. If your code is not working as expected, it could be due to compatibility issues with your computer's hardware or operating system. Make sure you are using the correct assembly language for your computer's architecture.

4. How can I improve the performance of my assembly language code?

There are a few ways to improve the performance of your assembly language code. One way is to optimize your code by minimizing the number of instructions and using efficient algorithms. Another way is to use advanced techniques such as parallel processing or optimizing for specific hardware features. Additionally, make sure to use proper coding practices and avoid unnecessary operations.

5. Is it possible to integrate assembly language with other programming languages?

Yes, it is possible to integrate assembly language with other programming languages. Some high-level languages, such as C or C++, allow you to insert assembly code within your program. This can be useful for optimizing critical sections of your code. Additionally, you can also use assembly language to call functions written in other programming languages, making it possible to combine their features and capabilities.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
20
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Programming and Computer Science
Replies
7
Views
6K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
20
Views
9K
  • Programming and Computer Science
Replies
11
Views
4K
Back
Top