http:^^www.cs.wisc.edu^~cs354-2^cs354^solutions^q1.j.html
来自「This data set contains WWW-pages collect」· HTML 代码 · 共 246 行
HTML
246 行
Date: Tue, 05 Nov 1996 20:51:37 GMTServer: NCSA/1.5Content-type: text/htmlLast-modified: Fri, 13 Sep 1996 18:53:11 GMTContent-length: 5710<!--This file created 9/12/96 3:31 PM by Claris Home Page version 1.0b1--><HTML><HEAD> <TITLE>Q1.m.html</TITLE> <X-SAS-WINDOW TOP=42 BOTTOM=397 LEFT=4 RIGHT=534></HEAD><BODY><H3 ALIGN=CENTER><B>CS354, Fall 1996</B></H3><H3 ALIGN=CENTER><B>Quiz 1, Sections 1 & 3</B></H3><H3><B>Name (printed): Name (signed):</B></H3><P> </P><P>(1) (2 points) What changes would need to be made to the SAL codeshown in figure 2.7 on page 39 of the text in order to get it to runcorrectly?</P><BLOCKQUOTE><P><B>ANSWER</B></P><DL> <DD>(a) add "ch: .byte" to the .data section, <DD>(b) change "loop" to "__start"</DL></BLOCKQUOTE><P>(2, ver 1) (4 points) Translate the following C statement into SALcode:</P><BLOCKQUOTE><P>A = 1 + B * ( A / B - 5 );</P><P><B>ANSWER</B></P><BLOCKQUOTE><PRE><TT><CODE>div temp, A, Bsub temp, temp, 5mul temp, B, tempadd A, 1, temp</CODE></TT></PRE></BLOCKQUOTE></BLOCKQUOTE><P>(2, ver 2) (4 points) Translate the following C statement into SALcode:</P><BLOCKQUOTE><P>A = B + 55 / ( A * B - 1 );</P><P><B>ANSWER</B></P><BLOCKQUOTE><PRE><TT><CODE>mul temp, A, Bsub temp, temp, 1div temp, 55, tempadd A, B, temp</CODE></TT></PRE></BLOCKQUOTE></BLOCKQUOTE><P>(3, ver 1) (2 points) Assuming "a" is of type ".word," what isprinted by the following SAL code:</P><BLOCKQUOTE><PRE><TT>move a, 468div a, a, 10put arem a, a, 10put a</TT></PRE><P><B>ANSWER</B></P><BLOCKQUOTE><P>466</P></BLOCKQUOTE></BLOCKQUOTE><P>(3, ver 2) (2 points) Assuming "a" is of type ".word," what isprinted by the following SAL code:</P><BLOCKQUOTE><PRE><TT>move a, 468rem a, a, 10put adiv a, a, 3put a</TT></PRE><P><B>ANSWER</B></P><BLOCKQUOTE><P>82</P></BLOCKQUOTE></BLOCKQUOTE><P>(4) (5 points) Explain the difference between "b x" and "b (y)"</P><BLOCKQUOTE><P><B>ANSWER</B></P><DL> <DD>"b x" causes a branch to the line of the code labeled by "x" while "b (y)" causes a branch to the line of the code whose address has been stored in variable "y"</DL></BLOCKQUOTE><P>(5, ver 1) (10 points) Write a complete SAL program that will addall of the even numbers from 8 to 72 except for multiples of 3. Theprogram should print the result and label it using the string "sum =." For example, if the sum were 1234, the program would print:</P><BLOCKQUOTE><P>sum = 1234</P><P><B>ANSWER</B></P><BLOCKQUOTE><PRE><TT><CODE> .datasum: .word # running sumn: .word 8 # n = 8 to 72, evenk: .word # k = n mod 3msg: .asciiz "sum = " .text__start: add sum, sum, nnext: add n, n, 2 # keep n even rem k, n, 3 beqz k, next # skip multiples of 3 ble n, 72, __start puts msg put sum putc '\n" done</CODE></TT></PRE></BLOCKQUOTE></BLOCKQUOTE><P>(5, ver 2) (10 points) Write a complete SAL program that will addall of the odd numbers from 9 to 105 except for multiples of 5. Theprogram should print the result and label it using the string "sum =." For example, if the sum were 1234, the program would print:</P><BLOCKQUOTE><P>sum = 1234</P><P><B>ANSWER</B></P><BLOCKQUOTE><PRE><TT><CODE> .datasum: .word # running sumn: .word 9 # n = 9 to 105, oddk: .word # k = n mod 5msg: .asciiz "sum = " .text__start: add sum, sum, nnext: add n, n, 2 # keep n odd rem k, n, 5 beqz k, next # skip multiples of 5 ble n, 105, __start puts msg put sum putc '\n' done</CODE></TT></PRE></BLOCKQUOTE></BLOCKQUOTE><P>(6, ver 1) (2 points) What is 3456 in Roman numeral notation?</P><BLOCKQUOTE><P><B>ANSWER</B></P><DL> <DD><TT>3000 + 400 + 50 + 6</TT> <DD><TT>MMM CD L VI = MMMCDLVI</TT></DL></BLOCKQUOTE><P>(6, ver 2) (2 points) What is 3607 in Roman numeral notation?</P><BLOCKQUOTE><P>ANSWER</P><DL> <DD><TT>3000 + 600 + 0 + 7</TT> <DD><TT>MMM DC VII = MMMDCVIII</TT></DL></BLOCKQUOTE><P>(7, ver 1) (10 points) Convert the following C code to theequivalent SAL code, assume all variables are of integer type.</P><DL> <DD><TT>if (a > b) </TT> <DL> <DD><TT>++a;</TT> </DL> <DD><TT>else if ( b == 5) </TT> <DL> <DD><TT>a = a * b;</TT> </DL> <DD><TT>else </TT> <DL> <DD><TT>a = 0;</TT> </DL> <DD><TT>printf("%d\n",a);</TT> <P><B>ANSWER</B></P> <BLOCKQUOTE><PRE><TT># SAL code is: ble a, b, else1 add a, a, 1 j prtelse1: bne b, 5, else2 mul a, a, b j prtelse2: move a, 0prt: put a putc '\n'</TT></PRE></BLOCKQUOTE></DL><P>(7, ver 2) (10 points) Convert the following C code to theequivalent SAL code, assume all variables are of integer type.</P><DL> <DD><TT>if (a < b) </TT> <DL> <DD><TT>a = a + 2;</TT> </DL> <DD><TT>else if ( b != 5) </TT> <DL> <DD><TT>a = a / b;</TT> </DL> <DD><TT>else </TT> <DL> <DD><TT>++a;</TT> </DL> <DD><TT>printf("%d\n",a);</TT> <P><B>ANSWER</B></P> <BLOCKQUOTE><PRE><TT># SAL code is: bge a, b, else1 add a, a, 2 j prtelse1: beq b, 5, else2 div a, a, b j prtelse2: add a, a, 1prt: put a putc '\n'</TT></PRE></BLOCKQUOTE></DL></BODY></HTML>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?