1. You should implement an emulator that execute an assembler program written in a very limited language. The processor you should emulate has a state described by: a code segment, a program counter and six registers (0..5). There is no memory and the code segment can only be read.
The emulator is initialized with the program counter set to zero (the first instruction) and proceed one instruction at a time if the program counter is not set by a branch instruction.
The instructions that the emulator should handle are listed below. In the pseudo code, reg[x] to the right means the value in register x. To the left in an assignment it means that the value should
be stored in register x. The output should be collected and returned as a list of values with the first outputted value first.
¡ñ ¡ñ ¡ñ ¡ñ ¡ñ
You can represent instructions, the program and registers in any way you want but you’re not allowed to use any library functions in your solution. You can use elem/2 and put_elem/3 but for example not the library function List.to_tuple/2.
Your solutions should include the coding of the program below and the result obtained when the program is executed.
add d s1 s2 : reg[d] := reg[s1] + reg[s2]
addi d s1 imm : reg[d] := reg[s1] + imm
beq s1 s2 offset : pc := pc + offset if reg[s1] == reg[s2]
out s1 : output reg[s1]
halt : terminate
addi 1 0 10
addi 3 0 1
out 3
addi 1 1 -1
addi 4 3 0
add 3 2 3
out 3
beq 1 0 3
addi 2 4 0
beq 0 0 -6
halt
2. You should implement the sieve of Eratosthenes to identify primes. You should in the end have a function that given an integer (n) returns a list of all primes up to, and including this number. The sieve should be implemented as a sequence of processes where each process is responsible for filtering out numbers divided by a given prime.
One way to implement this is to start a filter process and then send it messages with all numbers from 2 to n (one number at a time). A filter process knows that the first number it receives (p) is a
prime and this should some how be collected. When it receives the first number it will create a new filter process to which it will forward all number not divisible with p.
If you get everything to work the first process will filter out all numbers divisible by 2, the second all divisible by 3, the third all divisible by 5 etc. When the last number is sent the processes should terminate and all primes returned.
3.
Given a list of numbers (not empty), for example [2,4,5,3,1,3,2], you should calculate the smallest cost to divide the list into its parts: [2], [4], [5] ….. You can only divide the list in two in each “cut” and the cost of a cut is the sum of all numbers in the list.
As an example we can take the list [2,1,4] that first should be cut into [2,1] and [4] at the cost of 7 and then the list [2,1] is cut into [2] and [1] at the cost of 3. The total cost is thus 10 which is better than first cut it in [2] and [1,4] and then cut [1,4] in [1] and [4] which would add up to 12.
Since I am the worlds most kindest you’re given a program that calculates the least cost. To solve all the questions it could be that you need to change the program. Answer the questions and send in your version of the program that you used to solve the questions. You’re free to use any library functions.
def cut([_]) do 0 end
def cut([s|seq]) do cut(seq, [s]) end
def cut([l], right) do
cut(right) + l + Enum.sum(right)
end
def cut(left, right) do
alt1 = cut(left) + cut(right) + Enum.sum(left) + Enum.sum(right)
alt2 = cut(tl(left), [hd(left)|right])
if alt1 < alt2 do
alt1 else
alt2 end
end