?
Solved

Help Please, Urgent Assembler code

Posted on 1998-12-07
6
Medium Priority
?
355 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 400 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

770 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