Nios/loadexec

From ASMBits

Write a function that will copy a block of code (assumed to be a function) to a given location in memory, then call the function, passing it one integer parameter.

void loadexec( int(*dest)(int), int(*src)(int), unsigned int code_length, int param);

The block of code is located at location src, with length code_length bytes. This block of code should be copied to dest. After copying, execute the code at dest and pass it parameter param, and returning its return value (i.e., with dest as a function pointer, do return dest(param))

Expected solution length: Around 10 lines.

Sample Input

[See below]

Sample Output

r2=8

Write your solution here

x
 
1
# Some testing code
2
# When debugging in CPUlator, turn off the following debugging checks:
3
# - Instruction fetch: Modified opcode
4
# - Instruction fetch: Outside a code section (If Dest is outside the .text section)
5
6
Src: 
7
    add r2, r4, r4  # This function returns double its parameter.
8
    ret
9
Dest:
10
    .skip 8     # Make space for the code
11
12
.global _start
13
_start:
14
    movia r4, Dest
15
    movia r5, Src
16
    movia r6, 8 # There are 2 instructions to copy: 8 bytes
17
    movia r7, 4 # Let's call the function with parameter 4
18
    call loadexec
19
    1: br 1b  # done
20
21
# Your function starts here:
22
loadexec:
23
    
24
    
Upload a source file...