Reputation: 118
I have been given a small assignment to make a small program in assembly now I am trying to make a grade calculator you input your marks and it tells you the grade
.model small
.stack 100h
.data
Msg1 db 'Enter your total marks from 0 to 99: $'
EightyPlus db 'Congratulation you have got an A $'
.code
start:
Mov ax,@data
Mov ds,ax
Mov ah,09
Lea dx,Msg1
int 21h
mov ah,02
mov dl,10
int 21h
mov ah,01
int 21h
mov bl,01
int 21h
cmp al, '8'
jl exit
;; true1: ;; we don't really need this label anymore, as it is not referenced
mov ah,02
mov dl,10
int 21h
Mov ah,09
Lea dx,[EightyPlus]
int 21h
;; now continue and exit
exit:
Mov ah,4ch
int 21h
end start
Now what I want is that if al input is greater than or equal to 8 it should print grade A but as of now it prints grade A on every input need some help here
The bl
is just a gimmick to get two inputs from the user you can ignore it or help me to take more than one character input from user and do the task as I have asked above
EDIT: it was working through this code but now as I have used bl
it does not work again
Upvotes: 2
Views: 368
Reputation: 58805
mov al,01
int 21h
mov bl,01
int 21h
The DOS function to read a character from standard input is int 21h
with ah = 01h
. See http://www.ctyme.com/intr/rb-2552.htm. You have never initialized ah
. And the mov
to bl
in between is pointless.
It sounds like you want to read a character and use it for your comparison, then read a second character and ignore it. You could do something like:
mov ah, 01h
int 21h
mov bl, al
int 21h ; ah still contains 01, unless DOS is buggy
cmp bl, '8'
jge true1
A second bug is after jge true1
. Remember how a conditional jump works: it jumps if the condition is true, and if not, execution continues with the next instruction in memory. In your program, the next instruction is ... the instruction right after the true1
label. So execution continues at true1
regardless of whether the condition was true or false.
Sometimes beginners to assembly think that labels are like functions, and code will automatically "return" when it reaches a new label, as if it were the close brace of a function. That's not true; labels just give you a human-readable way to refer to a particular address, and don't by themselves affect program flow at all.
If you want to skip the output of the EightyPlus
string when the condition is false, then you need to jump over it:
cmp bl, '8'
jge true1
jmp exit
true1:
Mov ah,09
Lea dx,[EightyPlus]
int 21h
;; now continue and exit
exit:
Mov ah,4ch
int 21h
But you could do it more efficiently by reversing the test:
cmp bl, '8'
jl exit
;; true1: ;; we don't really need this label anymore, as it is not referenced
Mov ah,09
Lea dx,[EightyPlus]
int 21h
;; now continue and exit
exit:
Mov ah,4ch
int 21h
Upvotes: 1