Programming Problem [50 points] • write an LC-3 program that uses subroutines to
ID: 3602941 • Letter: P
Question
Programming Problem [50 points]
• write an LC-3 program that uses subroutines to convert an nonnegative integer to its representation
as a string of ASCII characters and then prints the string.
• Your main routine will be very short:
• LD R0 with the value stored at label X.
• JSR to a CONVERT subroutine that converts the value in R0 to its equivalent ASCII string and returns a
pointer to the string in R0.
• PUTS the string and HALT.
• Note you’ll only convert and print the one value stored at X. If you want to convert and print a different
value, you’ll have to modify the program and reassemble it first.
• The CONVERT subroutine should contain a buffer large enough to hold the string for the maximum positive
16-bit integer. It should fill the buffer from right-to-left with the characters for the digits of the value in R0
and return with R0 pointing to the string that was built. Eg., for the value 17, CONVERT should set the last
three positions in the buffer to '1', '7', and '' and return with R0 = the address of the '1'. (Note
there can be be unused buffer positions to the left of the '1'.)
• Here is some C-like pseudocode for the CONVERT subroutine:
Save registers
Set pointer p = &(last character of output buffer)
*p = ‘’
if R0 == 0
*--p = ‘0’
else if R0 < 0 {
print an error message
make sure we’ll return "" as the result
}
else {
set numerator to R0, denominator to ten
until numerator == 0 {
! ! Call DIVIDE subroutine
! ! *--p = '0' + remainder
! ! set numerator to quotient, denominator to ten
}
}
Ensure R0 = address of first character of string
Restore other registers
return (via JMP R7)
• The DIVIDE subroutine should take two integer arguments, a numerator and denominator. It should divide
the numerator by the denominator and return the quotient and remainder.
• You can assume without checking that the numerator 0 and the denominator > 0.
• [For extra credit, you can check for these being out of range. See grading guide below.]
• Set up your subroutine so that the numerator and denominator are passed to DIVIDE by having them
be in two registers. (Which ones are up to you.)
• Also use registers to hold the quotient and remainder values you’re returning. Again, which registers
are up to you. It’s also up to you to decide whether the numerator and/or denominator registers get
overridden by the quotient and/or denominator.
• Write DIVIDE so that it works with any positive denominator even though CONVERT will always call
DIVIDE with a denominator of ten.
• Use repeated subtraction to do the division. (If we had bit-shifting operations, we could write
something faster, but we don’t.)
Grading Guide [50 points total]
• Main routine
• [2 pts] Load X into the numerator register, set denominator register to 10
• [1 pt] JSR CONVERT
• [1 pt] PUTS result of CONVERT
• [1 pt] HALT
• [1 pt] Declare X .FILL some value
• CONVERT subroutine
• [1 pt] Save registers
• [2 pts] Establish pointer p and last character of output buffer
• [2 pts] if R0 == 0, set *p = '0' (The returned string should be "0".)
• [2 pts] If R0 < 0, print an error message and ensure we’ll return the empty string
• [2 pts] else
• [2 pts] Set numerator = R0; denominator = ten
• [2 pts] Until numerator == 0,
• [2 pts] Call DIVIDE subroutine
• [3 pts] Store '0' + remainder into the buffer and decrement p
• [2 pts] Set numerator = quotient, set denominator = 10
• [1 pt] Set R0 to address of first character of the built-up string
• [1 pt] Restore other registers and return
• DIVIDE subroutine
• [2 pts] Save registers, initialize quotient and remainder
• [2 pts extra credit] If numerator < 0 or denominator 0,
print an error message, set quotient and remainder to some default values
and skip the following loop
• [3 pts] Until remainder < denominator
• [3 pts] Increment quotient, subtract denominator from remainder
• [2 pts] Restore registers and return quotient and denominator
• Program Structure & Comments
• [2 pts] Name and section in comments
• [5 pts] Header comments for subroutines (should describe parameters and results and list used /
destroyed registers
• [5 pts] Sections well-organized, mnemonics, arguments, and line comments formatted and readable
Explanation / Answer
#include<stdio.h>
#include<conio.h>
#define P printf
#define S scanf
#define SIZE 20
void main()
{
// int sum_arr(int [],int);
int sum_arr1(int *,int);
int a[SIZE],n,i,s;
clrscr();
P("How many elements - ");
S("%d",&n);
for(i=0;i<n;i++)
{
P("Enter the value - ");
S("%d",&a[i]);
}
// s=sum_arr(a,n);
// printf("%d",s);
s=sum_arr1(a,n);
P(" %d",s);
getch();
}
/*int sum_arr(int a[],int n)
{
int s=0,i;
for(i=0;i<n;i++)
s=s+a[i];
return s;
}*/
int sum_arr1(int *p,int n)
{
int s=0,i;
for(i=0;i<n;i++)
s=s+*(p+i);
return s;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.