|
-
November 8th, 2005, 11:47 PM
#1
convert decimal input to english text output
hey guys
i've been working on this one lab forever, and i just cannot get it right... actually, i can't even really start:
Lab Description:
You are to write an LC-3 program that accepts a decimal number from the user and prints the
English text equivalent of that number. The input number must be positive and between 0 and 99.
For example, if the user enters the numbers 2 and 4, then your program will print (send to the
display) the string “twenty-four”.
Hence this lab will focus on I/O operations (GETC, OUT, and PUTS) and arrays of characters
(known as “strings”) and arrays of pointers. It is important to distinguish between a value (such
as the number 3) and the character that represents is (such as the character ‘3’), between a value
and a pointer to that value, between a single character and a string.
Your code will have several “segments” or sections. Follow these directions for the appropriate
segments of your code:
1. Character input and echo:
- Write the code to prompt the user (with a “?”) to input a single decimal digit from the console.
Use the GETC routine to input a character, echo it to the console (so that the user sees what he
typed), and test whether it is a decimal digit (‘0’-‘9’).
− If the input character is a decimal number, then copy the character to R1.
− If the input character is a carriage return (ASCII x0A), branch/jump to a label called “DONE”.
This means you need not prompt for and convert any more inputs.
− If the input character is anything else, print “Ilegal Number”, and branch/jump to the beginning
and get another character.
2. ASCII-to-binary conversion:
After the character input code is (from segment 1 above) is executed, the ASCII code for a decimal
digit is in R1. Convert and replace this ASCII character with its corresponding binary number. In
other words, if R1 contains ASCII ‘2’ (0000 0000 0011 0010), make it contain the binary number 2
(0000 0000 0000 0010). Make sure your assembly language code works for all decimal digits.
3. Indexing into an array:
An array is a sequence of items of the same type and size. For example, you might have an array
of integers that represents the lab grades of all students in your section (89, 97, 15, 100, 99, 36,
99). The position that each item holds in an array is called its index. As always, we start counting
with zero, so the index of 89 in the above array is 0, the index of 97 is 1, and the index of the first
99 is 4. A common operation is to retrieve the i-th element of an array, given the index i.
Consider an array of integers that starts at location x4000, and R1 contains the index of an
element that you want to use. Write assembly language code to load that array element into R2.
(Assume that R1 is a “legal” index – that is, it is non-negative and less than the size of the array.)
Assume that your code will be loaded into the memory page starting with location x3000, so you
will not be on the same page as the array.
As mentioned earlier, you will write a program that prints the English text equivalent of a
decimal number between 0 and 99. Additional specifications are detailed below:
1. Print a prompt (“?”) to the console that signals the user to provide input. This can be very
simple, such as a question mark followed by a space.
2. Read characters from the keyboard and echo them to the console, until a complete legal input
is received. There are three kinds of legal input:
−−− A single decimal digit followed by a carriage return. This will be interpreted as a one-digit
decimal number.
−−− Two decimal digits followed by a carriage return. This will be interpreted as a two-digit
decimal number.
−−− A carriage return by itself. This will cause the program to terminate.
Any other input is illegal. An illegal input causes an error string to be printed, and
the program returns to the beginning prompt.
3. If a decimal number was input, print the English text equivalent, such as “zero”, “twelve”,
“sixty-four”, or “ninety”. Also print a carriage return after the string, so that the next prompt
appears on a new line.
4. After the text is printed, return to the beginning prompt.
You must use the GETC and OUT routines to input and echo the characters. You must use the
PUTS routine to print strings for the prompt, the error message, and the text output. (That does
not mean that a single PUTS must be used to print the text output.)
TIPS:
--- Consider what is the address of the data item that we wish to load? How can we compute that
address?
--- Consider which load instruction (addressing mode) should we use to move the data from
memory to R2?
--- You do not need to combine the ones’ digit and tens’ digit into a single binary value to
complete this program. The tens’ digit can be used to determine the first part of the text
output (e.g., “thirty”) and the ones’ digit can be used to determine the second part (e.g.,
“three”).
---- Make sure you properly deal with the following cases: zero, numbers between 11 and 19,
numbers that end in zero. Don’t forget the hyphen in numbers like ninety-nine.
--- A useful idea is to use the input value as an index into an array. Since an array assumes
that all elements are the same size, don’t put strings into the array – put pointers in the
array. In other words, let the i-th element of an array hold the address of a string, instead
of the string itself.
i have managed to write a few lines of code, mere segments of the assignments.. but i can't connect them, and i'm not even sure they work properly:
Code:
;routine to check that the magnitude of input is between 0 and 99.
RangeCheck LD R5,ZERO
ADD R4,R0,R5
BRp BAD
LD R5,NINETYNINE
ADD R4,R0,R5
BRn BAD
ADD R5,R5,0
RET
BAD ST R7,Save
LEA R0,ERROR
PUTS
LD R7,Save
AND R5,R5,#0
ADD R5,R5,#1
RET
ZERO .FILL #0
NINETYNINE .FILL #99
Save .FILL x0000
ERROR .FILL x000A
.STRINGZ "Error: Must be number between 0 and 99."
;input decimal digits from keyboard, convert ascii into binary, store binary values in successive memory locations
;starting at the address Binary.
LEA R3,Binary
LD R6,ASCII
AGAIN GETC
OUT
ADD R1,R1,R6
STR R1,R3,#0
ADD R3,R3,#1
ADD R7,R7,#-1
BRp AGAIN
BRnzp DONE
DONE HALT
ASCII .FILL xFFD0
Binary .BLKW #1
ur help will be much appreciated
If you choke a smurf, what color does it turn?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|