Solved

Help Please, Urgent Assembler code

Posted on 1998-12-07
6
323 Views
Last Modified: 2010-04-02
I have been working on this program for the past two days, and I now mired in my own code. Could someone please help me to make this program work?

Here is the object of the program:

To write a program that will prompt the user to enter a character. Only numbers 0 to 9 and uppercase letters A-Z as well as lowercase letters a-z are considered valid entries. The program will terminate once the user inputs a CR. The program then outputs home many numbers and letters the user entered.

My problem of course is MSG3 and the coding to implement it.

Here is the code I have come up with until now. Of course, I am a newbie at this and find assembler difficult.

TITLE : LOOP ENTER CHARS AND LETTERS

.MODEL SMALL
.STACK 100H
.DATA

LF EQU OAH      ;Line Feed OAH is equal to LF
CR EQU ODH      ;Carriage return ODH is equal to CR

MSG1      DB LF, CR, 'PLEASE ENTER A CHARACTER (NUMBER OR LETTER) : $'
NUMBER      DB ?, '$'
LETTER      DB ?, '$'
MSG2      DB LF, CR, 'INVALID ENTRY $'
MSG3      DB LF, CR, 'YOU HAVE ENTERED NUMBERS $' 'AND LETTERS : $'

.CODE

MAIN PROC
MOV AX, @DATA
MOV DS, AX

WHILE:
CMP AL, CR
JE END_WHILE

; Display the initial string
LEA DX, MSG1
MOV AH, 9      ;Display the first message
INT 21H

; Read the character
MOV AH, 1      ; into AL
INT 21H

;Check if it falls within the 0-9 range?
; (i.e., is there only one digit to be displayed?)

CMP AL, '0'
JB INVALID
CMP AL, '9'      ;Compare AL
JA AZ_RANGE      ;Jump if above to Az_RANGE
JMP NUMBERS      :Else Jump a valid number

;Otherwise does it fall within the A-Z range?
; (i.e., Does it contain 2 digits?)

AZ_RANGE:
CMP AL, 'A'      ;compare what is in AL
JB INVALID      ;If it is below A in the ascii code it is invalid
CMP AL, 'Z'      ;If lower or equal to letters
JLE LETTERS      ;Jump if lower of equal

INVALID:
LEA DX, MSG2
MOV AH, 9      ;display Msg2 Invalid entry
INT 21H
JMP WHILE_

NUMBERS:
MOV NUMBER, AL
LEA DX, MSG1
MOV AH, 9
INT 21H
JMP WHILE_

LETTERS:
MOV LETTER, AL
LEA DX, MSG1
MOV AH, 9
INT 21H
JMP WHILE_

INC CX
JMP WHILE_

END_WHILE:
MOV AH, 4CH
INT 21H            ;Terminate program

MAIN ENDP
      END MAIN

Thank you for you help.
0
Comment
Question by:grateful_1
  • 3
  • 2
6 Comments
 
LVL 1

Expert Comment

by:MatthewL
ID: 1179519
Hi grateful_1

>My problem of course is MSG3 and the coding to implement it.

I assume the concern is how to print out an integer?  If so then you need to do repeated division by base 10.

A safe bet is to assume a max place value of 10^4 ( max unsigned int 2^16-1 ).

repeatedly divide your number by the place value, printing the quotient and using the remainder for the next division.  This bottoms out when the place value is 1.
When you go to print the value, add ascii 0 to it first.
( ADD DL,'0' ).
e.g.

134 / 10,000  is  0 remainder 134     leading zero so don't print
134 / 1,000   is  0 remainder 134        "
134 / 100     is  1 remainder 34      print a 1
34 / 10       is  3 remainder 4       print a 3
4 / 1         is  4 remiander 0       print a 4

P.S.  You are using JLE in your code.  This will work on characters since they only use 7 bits, but the lower and greater jumps are meant for signed ints, the above and below ones are meant for unsigned ints - you should use JBE

Hope this helps,

Matt
0
 
LVL 1

Accepted Solution

by:
billyh earned 100 total points
ID: 1179520
Hi, just copy and paste the following code.

MODEL  SMALL
STACK  100H
DATA

CR   EQU 0DH
LF   EQU OAH

MSG1 DB LF, CR, 'PLEASE ENTER A CHARACTER(NUM OR LETTER) : $'
MSG2 DB LF, CR, 'INVALID ENTRY $'
MSG3 DB LF, CR, 'ENTERED LETTERS: $'
MSG4 DB LF, CR, 'ENTERED NUMBERS: $'

KEY_P DB ?
LETTER DB ?, '$'
NUMBER DB ?, '$'
VALID_K DB 1

CODE

DISPLAY MACRO DISPTXT
MOV  AH, 9
LEA  DX, DISPTXT
INT  21H
ENDM


GET_KEY PROC
MOV  AH,1
INT  21H
MOV  KEY_P,AL
RET
GET_KEY ENDP


ANAL_KEY PROC
MOV VALID_K,1
CMP KEY_P,30H ;; CHECKS FOR ZERO
JL  @@SETZ
CMP KEY_P,39H ;; FOR NINE
JG  @@TEST
JMP @@OKAY    ;; ITS A NUMBER

@@TEST:
;; CHECKS FOR UPPER-CASE
CMP KEY_P,41H
JL  @@SETZ
CMP KEY_P,5AH
JG  @@TEST_2
JMP @@OKAY

@@TEST2:
;; CHECKS FOR LOWER-CASE
CMP KEY_P,61H
JL  @@SET_Z
CMP KEY_P,7AH
JG  @@SET_Z
JMP @@OKAY

@@SET_Z:
MOV VALID_K,O
@@OKAY:
RET
ANAL_KEY ENDP


UPDATE PROC
CMP KEY_P,39H
JG  @@LETTER
ADD NUMBER,1
JMP @@FIN

@@LETTER:
ADD LETTER,1
@@FIN:
RET
UPDATE ENDP


MAIN PROC
MOV AX,@DATA
MOV DS, AX

@@ACTIVE:
CALL GET_KEY
CMP  KEY_P,CR
JE   @@TERMINATE
CALL ANAL_KEY
CMP  VALID_K,1
JNE  @@NOTIFY
CALL UPDATE
JMP  @@ACTIVE

@@NOTIFY:
DISPLAY MSG2
JMP  @@ACTIVE

@@TERMINATE:
DISPLAY MSG3
DISPLAY LETTER
DISPLAY MSG4
DISPLAY NUMBER
MOV  AX,4CH
INT  21H

MAIN ENDP
END MAIN
0
 

Author Comment

by:grateful_1
ID: 1179521
Hi Billy,

Thank you for your response to my problem. As I mentioned I am a newbie to Assembler, so I'm afraid I did not quite understand the code you so kindly gave. Compared to the code I had sent, and yours, what where the errors that I could have changed in mine... Could you please indicate what my errors where so that I may understand what the problem was.

Again my thanks to you all.

grateful_1
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:grateful_1
ID: 1179522
Can someone please help with the previous comment?

Thank you.

0
 
LVL 1

Expert Comment

by:billyh
ID: 1179523
Okay, the problem with MSG3 is that you were going to print out this message to the screen;

YOU HAVE ENTERED NUMBERS AND LETTERS :

You problem here was going to be how to write the entered number. To do this you would have to keep the position, especially the row position of where MSG3 was written on the screen. You would then have to calculate the column position of where to write on the screen.

This would have been confusing and difficult for you, especially since you are new to assembler.

In my code, what I just did was to display the entered numbers in a different row and enters letters in different rows. The rest of the code I used procedures and macroes ro get you to know more of Assembler Programming. Infact Assembler is not as hard as people belive it to be, it just requires you to think differently.

Hope this helps.

0
 

Author Comment

by:grateful_1
ID: 1179524
Thank you very much. Yes it did help me, and yes I had discovered your source code included macros. Although, it is a difficult language to understand, I can see it has great possibilities.

Again, I thank you for your help.

grateful_1
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now