首页 > > 详细

Writing Python Assignment,data structure Programming Help With,Help Python Course,data structure E

This is the first of the ‘weekly’ assessed exercises you will do as part of the G51SYS course. There
are four parts to this exercise, the first gets you to implement a program that can calculate the
factorial of a number stored in memory, the second gets you to output the factorial of a series of
numbers, the third reads in a number entered by the user in hexadecimal and prints out the
decimal for the number entered and, finally, the fourth program gets you to implement a simple
random number generator. The next set of exercises will reuse parts of these exercises, so don’t
delete your source code.
You will need to fork and clone the relevant project via git in the usual fashion. In this case, the
project is G51SYS 2017/2017-SYS-Assessed01. Once cloned, you will find skeleton .s files for
each exercise. It is strongly recommended that you use these skeleton otherwise your programs
may not work against the marking test scripts.
Note for all these programs you may well find it useful to start by generating a C version first to get
the logic of the program sorted before you start transliterating it into ARM assembler.
Factorial
Write a program that can calculate the factorial of a number, stored in the predefined variable x. In
other words, if x contains the value 5, you should calculate 5 x 4 x 3 x 2 x 1.
Once calculated, you should print out the result in the following manner before stopping your
program (substituting the correct value of x instead of 5):
The factorial of 5 is 120.
Make sure you test your program with various values of x!
Hint: Remember the MUL and MLA instructions require all operands to be in registers.
Factorial Table
This program is very similar to the first, except it now prints the factorial of all the numbers up to
the value in x. So if, x contains the value 5, you should print out:
The factorial of 1 is 1.
The factorial of 2 is 2.
The factorial of 3 is 6.
The factorial of 4 is 24.
The factorial of 5 is 120.
Again, make sure you test your program with various values of x to check it works correctly.
INDIVIDUAL WORKASSESSED
Hexadecimal Input
In a previous exercise, you wrote a program that read in a single digit from the keyboard. For this
exercise, your program should ask the user to enter an integer number in hexadecimal.
Unfortunately, Komodo only provides a mechanism (SWI 1) to read raw characters from the input,
not integers, so you will need to write your own routine to convert this into an integer number.
Your routine will need to do the following:
• Print out a prompt for the user asking them to enter a hexadecimal number
• Repeatedly read characters from the keyboard and if they are hex digits convert it from an
ASCII code into a number between 0 and 15 (remember, ASCII lays out the digits ‘0’—‘9’ in
order with the codes 48–57, and the alphabetic characters ‘A’—‘F’ in order from 65 – this
makes it relatively easy to go from the ASCII code to the integer value with a simple
subtraction). So if the value returned from SWI 1 is the ASCII code for B, you’ll need to convert
this into the number 11, while if it is the ASCII code for ‘5’, you’d need to convert this into the
number 5.
• Note that SWI 1 doesn’t print out the character input, so you will need to print it out for the
user to see what they typed…
• Build up the integer being typed from the digits being typed (remember that each digit
entered represents a power of sixteen entered so repeatedly multiplying by 16 is your friend
here…)
• The number is ended when the return/enter key is pressed (ASCII code 10). Any other
characters entered should cause an error message to be printed (on a new line), and the input
restarted by printing the prompt again.
• Store the integer entered into a memory location and then print it in decimal out using SWI 4.
Hint: Build this up in stages, e.g. start by just reading keys until return is pressed, then handle
building up the integer, then add the error handling etc.
Random Number Generator
For the final exercise this week, you are to implement a simple Pseudo-Random Number
Generator. Your program will be based on IBM’s RANDU generator which is ‘wildly considered to
be one of the most ill-conceived random number generators ever designed’…
However, it is relatively straight forward to implement in assembler since it is based on a simple
multiplication and taking the modulus of a power of two. The random number is based around the
formula:
V
j+1
= 65539 V
j
mod 2
31

This means that the next random number we generate (V
j+1
), is created by taking the previous
random number (V
j
) and multiplying it by 65539 and then taking the modulus of dividing that result
by 2
31
.
You are to write a program that repeatedly generates random numbers using the above formula,
and prints them out — one per line. However, you should:
• Loads the initial starting value for V
j
from a memory location (this should be called seed). You
should experiment to see how different initial seed values generate different sequences of
numbers — but don’t use zero, it won’t work… This is known as the seed for the random
number generator. Each seed will generate a different series of random numbers, but resetting
the seed to the same value will always generate the same sequence of nubmers.
• Once it has generated a random number, it should store the generated number back in seed.
This isn’t strictly necessary here (we could keep it in a register) but will be for how we reuse this
code in a later exercise.
• You’ll need to take the modulus of the result with 2
31
but there is no modulus instruction in
ARM. Fortunately, the modulus of any power of two can be found by doing a bitwise-and (using
the AND instruction) with 2
n
-1. In this case, 2
31
-1, is 0x7FFFFFFF in hex. Hence, AND-ing the
result of the multiplication with 0x7FFFFFFF will generate the result of the modulus.


It is left to the reader to work out why this is the case…
• Your program should loop infinitely…
 

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!