开发者

how did i do it in my exam :) counting the number of keypresses

i got this question in my exam today and i wrote some code for it.i would like you to tell me where did i go wrong.

Problem: for an 8086 microprocessor,write code that does the following task. it asks for user to enter the data and it counts the keypresses..

on execution of the ALP,the first message is obtained "counting keypresses , press escape to stop." (without quotation marks) , if the user enters the escape, the second message "counting keypresses" is shown and the result of the count is stored in BX and shown in displayed in binary. also the k开发者_高级运维eys pressed (while entering data) ,the input key shall be echoed in the next line.

Solution: (that i wrote)

.model tiny

.data

text1 db "counting keypresses,press escape to stop $"

text2 db "counting keypresses"

.code

.startup

mov dx,offset text1 ;displaying text1 (i guess so) mov ah,09h

int 21h

and bx,00h

label1 mov ah,01h ;(getting input from user and incrementing bx on every click) int 21h

inc bx

cmp al,1bh ;comparing with 'escape' np label1

mov dx,offset text2 ;if zero then do this i.e display text2 mov ah,09h

int 21h

mov dx,offset bx ;display bx mov ah,09h

int 21h

.exit

end

i am not sure if the comments i added are suitable or not. i know my code wont be correct,infact it wont be correct at all,but i managed to write this on the basis of my one night preparation.so if anyone could tell me,what wrong i am doing,then i will be highly thankful. because i really need to perform well in the next exam.


You have the basic idea, although there are a few details.

First, you have this:

cmp al, 1bh
np label1

I'm not sure what np is supposed to mean. The instruction you want there is probably jnz label1. That will branch if the Z flag is not set, meaning that the user didn't press ESC.

You have the following to display the count:

mov dx,offset bx ;display bx
mov ah,09h
int 21h

That's not going to work. In fact, that shouldn't compile, since you can't take the offset of a register. You need to store the value from BX into memory, and then pass that memory address to the output function. So define a 2-byte value in your data area:

count dw 0  ; counter for output
      db '$' ; terminate output

Then you can write:

move [count],bx
mov dx, offset count
mov ah, 09h
int 21h

You did say that you want to output the count in binary, but I'm not sure you know the ramifications of that. If the user presses only two keys, the output will be a binary 0 and a binary 2, which might display as boxes, or nothing at all, or perhaps funky characters. I don't remember how the int21 functions interpret all the control characters. For sure, though, if the user presses 13 keys, all you're going to get is a carriage return. And if the user presses 36 characters, the output will be ... nothing. Because 36 is the ASCII code for '$', and that's the output terminator character.

If you want to do a better job of output, you'll need to convert the value in BX to hexadecimal or decimal (hex is easier) to ASCII characters, and output those. I don't have a sample at hand.

Finally, I don't recall what the convention is for int21 functions saving registers. Is BX preserved across calls? If not, you'll need to push bx before calling any int21 function, and pop bx when it returns. Otherwise your count is going to be ... "undefined".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜