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

Click For Summary

Discussion Overview

The discussion revolves around a user's assembly language code intended to convert lowercase strings to uppercase and prompt the user for continuation. Participants explore issues related to conditional branching, code structure, and the inclusion of external files in the assembly code.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant describes their assembly code and expresses frustration that the program stops regardless of user input.
  • Another participant identifies a problematic line of code where a comparison is made followed by an unconditional exit, suggesting the need for a conditional branch.
  • A participant requests clarification on how to implement a conditional branch in assembly language.
  • Another participant explains the use of various jump instructions (e.g., jz, je, jne) and provides an example of how to structure conditional branches.
  • A participant modifies their code to handle invalid user input by prompting the user repeatedly until valid input is received.
  • One participant suggests including carriage return and line feed characters in print statements to ensure proper formatting in output.
  • A participant expresses confusion about how to include external files in their code and seeks advice on replacing the include statement.

Areas of Agreement / Disagreement

Participants generally agree on the need for conditional branching in assembly language, but there are differing opinions on how to implement certain aspects of the code, such as handling user input and including external files. The discussion remains unresolved regarding the best practices for including external libraries.

Contextual Notes

Participants mention specific assembly language instructions and conventions, but there are unresolved questions about the implications of using certain includes and how they affect program behavior.

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
 
Technology 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
 
You left out the 0dh and 0ah in the print statements. Try 0dh, 0ah, ' ...' to see if that's allowed.
 
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! =)
 

Similar threads

  • · Replies 20 ·
Replies
20
Views
6K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 7 ·
Replies
7
Views
7K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 11 ·
Replies
11
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
20
Views
10K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K