Do You Need Help with Programming Homework?“How do I pay someone to do programming homework on my behalf.” This question crosses the mind of every scholar who intends to find programming help online. If you have tried searching for an academic assistant over the net of late, you will notice that the...
Converting Temperature from Celsius to Fahrenheit in LC-3 Assembly Code
For this lab, we will write the LC-3 assembly code to convert temperatures given in Celsius to Fahrenheit. We need to give relevant messages to the user to guide him in using the program, for this, we make use of the TRAP x22, which displays a string on the console. Also, we require reading input from the user one character at a time, we make this by using the TRAP x23, which reads on the character from the keyboard and saves it in R0. The characters given by the user are represented in ASCII, so we must convert them to numbers before we can use them, since we know that the character for zero ‘0’ in ASCII has a value of 48, we only need to subtract this value to the read character to get the actual value. On the other hand, if we wish to display a number between 0 and 9, we need to add the 48 before we can use the TRAP x21 to display it in the console. We have a main loop that prompts the user for a selection and reads a character, then we compare the read number with 1, 2, or 3 to see which option was selected, we use simple subtraction to make the comparison, if the subtraction is zero, then that was the option selected and we branch to the part that handles the option. We have three parts to handle the input from the user; the first one is called Conv, which is executed when the user selection is 1. In this option, we prompt the user again to input another number, which will be the value to convert to Fahrenheit. In our approach, we decided to use a table to get the conversions from Celsius to Fahrenheit. The table that provides the conversion from the values 0 to 9 to the corresponding Fahrenheit temperatures is the following:
0 | 32 |
1 | 33 |
2 | 35 |
3 | 37 |
4 | 39 |
5 | 41 |
6 | 42 |
7 | 44 |
8 | 46 |
9 | 48 |
Thus, the number given by the user is used as an offset on the table and we simply load the value from the given offset to get the conversion. The second part is called NoConv, which is executed when the user selects option 2. In this option, we also prompt the user to input a number but this time we don't make a conversion but simply print the input number on the console. In both the Conv and NoConv options, the converted number or the number given by the user, respectively, is saved at x3100. The last part is the EXIT, which is executed when the user selection is 3. In this case, we simply print a message and halt the program. The resulting program is included below: .ORIG x3000 ;set origin of program to x3000 LOOP LEA R0, prompt; prompt for the user to select a number TRAP x22; trap to output the string TRAP x23; read a character LD R1,nascii0; load negative of ASCII 0 to convert the read character ADD R0, R0, R1; convert character to a number IF1 ADD R1, R0,#-1; see if the user entered a 1 BRz Conv; if it was 1, convert IF2 ADD R1, R0,#-2; see if the user entered a 2 BRz NoConv; if it was 2, do not convert IF3 ADD R1, R0,#-3; see if the user entered a 3 BRz EXIT; if it was a 3, exit Conv LEA R0, ask temp; prompt for the user to give a temperature TRAP x22; trap to output the string TRAP x23; read a character LD R1,nascii0; load negative of ASCII 0 to convert the read character ADD R0, R0, R1; convert character to a number LEA R1, table; point to the conversion table with R1 ADD R1, R1, R0; add the required temperature as offset LDR R0,R1,#0 ; load value from the table STI R0, address; save the number in x3100 BR LOOP Nov LEA R0,asktemp; prompt for the user to give a temperature TRAP x22; trap to output the string TRAP x23; read a character LD R1,nascii0; load negative of ASCII 0 to convert the read character ADD R0, R0, R1; convert character to a number STI R0, address; save the number in x3100 LD R1,ascii0; load ASCII 0 to convert the number for display ADD R0,R0,R1 ; convert to ascii TRAP x21; put the character on the console LD R0, NL TRAP x21; put the character on the console BR LOOP EXIT LEA R0,exitmsg ; print exit message TRAP x22; trap to output the string HALT ; halt the program prompt .STRINGZ "Enter 1 for converting to the Fahrenheit, 2 for no conversion, 3 for exit: " asktemp .STRINGZ "Enter the temperature in Celsius: " exitmsg .STRINGZ "Have a nice day" nl .FILL 10 ; new line character nascii0 .FILL -48 ; negative of ascii '0' ascii0 .FILL 48 ; ascii '0' address .FILL x3100 ; address to save conversions table .FILL 32 ; conversion table .FILL 33 .FILL 35 .FILL 37 .FILL 39 .FILL 41 .FILL 42 .FILL 44 .FILL 46 .FILL 48 .END A test using the input given in the homework specification results in the following output: