ENGN2219/COMP6719
Computer Architecture & Simulation Ramesh Sankaranarayana Semester 1, 2020
1
ENGN2219/COMP6719 Computer Architecture & Simulation Ramesh - - PowerPoint PPT Presentation
ENGN2219/COMP6719 Computer Architecture & Simulation Ramesh Sankaranarayana Semester 1, 2020 1 Course Revision 2 Final exam Final exam details and practice exams are up on the course website 3 Examinable material The examinable
1
2
3
4
5
6
7
8
9
10
11
12
13
import random # Function takes in the probability of heads as a number betwee # 0 and 1, and the number of tosses. Returns the average of the # number of heads over the number of tosses. def cointoss(headsprob, numtosses): heads = 0 for toss in range(numtosses): currentoss = random.random() if (currentoss <= headsprob): heads += 1 return(heads/numtosses)
14
# Print this value for say, 1000 tosses print (cointoss(0.5, 1000))
15
16
import numpy as np from scipy.interpolate import interp1d import matplotlib.pyplot as plt height = [0,600,1500,2300,3000,6100,7900]; temp = [100,98.8,95.1,92.2,90,81.2,75.6]; flinear = interp1d(height,temp) fcubic = interp1d(height,temp,kind='cubic') heightnew = np.linspace(0,7900,num=100) plt.plot(height,temp,'o',heightnew, flinear(heightnew), '-', he plt.legend(['data', 'linear', 'cubic-spline'], loc='best') plt.title('Linear and Cubic Spline Interpolation') plt.show()
17
18
19
import numpy as np from scipy.interpolate import interp1d import matplotlib.pyplot as plt from pylab import * week = [1,3,5,7,9,11,13]; height = [22,51,127,202,227,248,252]; z2 = np.polyfit(week,height,2) # second order polynomial f2 = poly1d(z2) heightfit2 = f2(week) sse2 = round(((height - heightfit2)**2).sum(),2) z3 = np.polyfit(week,height,3) # third order polynomial f3 = poly1d(z3)
20
heightfit3 = f3(week) sse3 = round(((height - heightfit3)**2).sum(),2) z10 = np.polyfit(week,height,10) # tenth order polynomial f10 = poly1d(z10) heightfit10 = f10(week) sse10 = round(((height - heightfit10)**2).sum(),2) plt.plot(week,height,'o',week, heightfit2,'-',week,heightfit3,' plt.legend(['data', 'second', 'third', 'tenth'], loc='best') plt.title('Sunflower Growth') plt.text(1,1,f'SSE2 = {sse2}') plt.text(5,1,f'SSE3 = {sse3}') plt.text(9,1,f'SSE10 = {sse10}') plt.show()
21
22
23
import numpy as np from scipy.interpolate import interp1d import matplotlib.pyplot as plt from pylab import * x = linspace(-2*np.pi,2*np.pi,180); y = 50*np.sin(x) + 3*np.cos(x) - 5*x**2 - x - 30; deriv_y = np.diff(y)/np.diff(x);
24
xd = x[0:-1]; # forward difference x values plt.plot(x,y,'b',xd,deriv_y,'r') plt.title('Polynomial-Trig Function') plt.ylabel("y and y'"); plt.xlabel('x') plt.legend(['y',"y'"],loc='best') plt.show();
25
26
h
27
28
import numpy as np from scipy import integrate import matplotlib.pyplot as plt from pylab import * def gravity(altitude): # Gravitational acceleration function # altitude in km; gravity in ms^-2 # Earth's radius (km) R = 6371; # Mean gravitational acceleration @ ground-level g0 = 9.81; return((R**2)*g0)/((R+altitude)**2);
29
# given parameters # mass (kg) of satellite m = 500; # height (km) from Earth's surface h = 800; gpevariation = integrate.quad(gravity,0,h); gpeChange = m*gpevariation[0]; #print(m,h); #print(gpeChange); print("The difference in gravitational potential energy of the
30
31
mport numpy as np from scipy.integrate import odeint import matplotlib.pyplot as plt #from pylab import * # given relationship: # m * dv/dt = -mg + kv^2 # rearrange into dv/dt = (-mg + kv^2)/m # m=5kg, g=9.81ms^-2, k=0.05 # def velocityOfObject(v,t): return ((-5*9.81)+(0.05*(v**2)))/5;
32
v0 = 0 t = np.linspace(0,15) v = odeint(velocityOfObject,v0,t) plt.plot(t,v) plt.title('Velocity of Falling Object') plt.xlabel('t') plt.ylabel('v') plt.show();
33
34
35
36
37
38
39
a+1 in big-endian and little-endian formats?
if-then-else statement labels in assembly? Assume that x is in register
r0 and the result y should be in the same register r0.
40
41
42
.syntax unified .global main @ READ ALL INSTRUCTIONS CAREFULLY @ You've been provided a data structure below for characters @ in a fantasy video game, similar to the ones presented in @ lecture slides. The data structure is as follows: @ Party size 2 bytes (stores the number of players in the pa
43
@ Followed by a structure for each player, as follows: @ HP 2 bytes (stores health points of the character @ Mana 2 bytes (stores the mana points of the character us @ Alignment 4 bytes (stores the alignment of the player as a SI @ (-3 = Very Evil, -2 = Evil, -1 = Weak Evil, 0 = Neutral, 1 = @ Name 16 bytes (stores the name of the character as a nul @ are added so the length is always @ Class 16 bytes (stores the class of the player as a null @ are added so the length is always @ Each player takes exactly 40 bytes in total to store.
44
@ Each player has a player ID, starting from zero, based on the @ In the example below: @ Player: ID: @ Astrid 0 @ Ulric 1 @ Zendril 2 @ Azar 3 @ See the .data section for examples
45
@ Question 1 : Party Disagreement Score @ Party members of vastly different alignment don't get along w @ a party comprised of both good-aligned and evil-aligned party @ can affect how well the party can work together as a team. @ The party disagreement score is defined to be the difference @ between the highest alignment and lowest alignment party memb @ For example, given the party in the .data section below, @ The highest alignment is Azar, with an alignment of 3 (Very G @ The lowest alignment is Zendril, with an alignment of -1 (Wea @ The disagreement score is therefore 3 - (-1) = 4
46
@ Write a function "disagreement" @ INPUTS: (in r0) the address of the party data in memory @ OUTPUTS: (in r0) the disagreement score for that party @ Your function should work for any party, of any size. @ DO NOT HARDCODE THE ANSWER main: ldr r0, =party bl disagreement end: nop b end
47
disagreement: @ put your code here nop bx lr
48
@ You may NOT add labels in the data section @ to make it easier to read information directly out. .data party: .hword 4 @party_size .hword 21 @ HP .hword 117 @ MANA .word 2 @ Alignment .ascii "Astrid Gold\0\0\0\0\0" @Player ID 0 .ascii "Priestess\0\0\0\0\0\0\0"
49
.hword 0 @ HP .hword 0 @ MANA .word 0 @ Alignment .ascii "Ulric Strongarm\0" @Player ID 1 .ascii "Mercenary\0\0\0\0\0\0\0" .hword 67 @ HP .hword 31 @ MANA .word -1 @ Alignment .ascii "Zendril Silver\0\0" @Player ID 2 .ascii "Dark Elf\0\0\0\0\0\0\0\0"
50
.hword 84 @ HP .hword 12 @ MANA .word 3 @Alignment .ascii "Azar the Worthy\0" @Player ID 3 .ascii "Paladin\0\0\0\0\0\0\0\0\0"
51
.syntax unified .global main main: ldr r0, =party bl disagreement end: nop b end disagreement:
52
@ Note that for this code, the disagreement score for a par @ Doesn't really matter, as the behaviour can be undefined @ The score is only defined for parties with one or more pl
53
@ r0 = address of current player, and at the end, return va @ r1 = current lowest alignment @ r2 = current highest alignment @ r3 = party size @ r4 = scratch mov r1, 100 @ dummy value always higher than any alignment mov r2, -100 @ dummy value always lower than any alignment ldrh r3, [r0], 2 @ r0 now points at start of first playe
54
disagreement_loop: cmp r3, 0 @ if no more players beq disagreement_calculate @ compute disagreement score ldr r4, [r0, 4] @ load player alignment cmp r4, r1 bgt skip_set_low_align @ skip if player alignment > lo mov r1, r4 skip_set_low_align: cmp r4, r2 blt skip_set_high_align @ skip if player alignment < hi mov r2, r4
55
skip_set_high_align: add r0, 40 @ move to next player sub r3, 1 @ decrement player counter b disagreement_loop disagreement_calculate: sub r0, r2, r1 @ score = highest - lowest bx lr
56
function gcd(a,b): while(a != b): if(a > b): a := a - b else: b := b - a return a
57
58
.syntax unified .global main main: @ Task 1: Read the bytes from `numbers` and save them into R0 nop gcd: @ Task 2: Compute the gcd of R0 and R1, store the result in R nop end: b end .data numbers: @Make sure you code works for other numbers too! @ You may change the numbers here. .byte 15, 21 @ gcd(15,21) = 3
59
.syntax unified .global main main: @ Task 1: Read the bytes from `numbers` and save them into R0 ldr r2, =numbers ldrb r0, [r2] ldrb r1, [r2, #1] gcd: @ Task 2: Compute the gcd of R0 and R1, store the result in R @ gcd: @ r0 := a @ r1 := b cmp r0, r1 @ while(a != b) beq end bpl change_a @ if(a >= b) then goto change_a
60
change_b: @ change_b: sub r1, r1, r0 @ b := b - a b gcd @ goto gcd change_a: @ change_a: sub r0, r0, r1 @ a := a - b b gcd @ goto gcd end: @result is already in r0 b end .data numbers: @Make sure you code works for other numbers too! .byte 15,21 @ gcd(15,21) = 3
61
62
.syntax unified .global main main: task1: @ Task 1: Read from memory address 'range', @ and store the numbers into R1 and R2. nop task2: @ Task 2: Compute the sum from R1 to R2, and save the resul nop
63
end: b end .data range: @ You can modify these numbers to test your code. .hword 4, 20 @ R0 should contain 4 + 5 + … + 19 + 20 = 204 @.hword 1, 5 @ R0 should contain 1 + 2 + 3 + 4 + 5 = 15 @.hword 3, 7 @ R0 should contain 3 + 4 + 5 + 6 + 7 = 25
64
.syntax unified .global main main: task1: @ Task 1: Read from memory address 'range', @ and store the numbers into R1 and R2. nop @ r0 will hold the result mov r0, 0
65
@ Read the numbers ldr r0, =range ldrh r1, [r0] ldrh r2, [r0, #2] @ ensure that r1 has the lower value cmp r1, r2 @ continue to task2 if r1 <= r2 ble task2 @ Interchange the values in r1 and r2 mov r3,r1 mov r1,r2 mov r2,r3
66
task2: @ Task 2: Compute the sum from R1 to R2, and save the resul @ Add the current number in r1 to r0 add r0, r1 @ Check if we are done cmp r1, r2 beq end @ Else, add 1 to r1 and go to task2 add r1, #1 b task2
67
end: b end .data range: @ You can modify these numbers to test your code. .hword 4, 20 @ R0 should contain 4 + 5 + … + 19 + 20 = 204 @.hword 1, 5 @ R0 should contain 1 + 2 + 3 + 4 + 5 = 15 @.hword 3, 7 @ R0 should contain 3 + 4 + 5 + 6 + 7 = 25
68
69
70