汇编代写: ARM asm

汇编代写:这个一个针对arm平台的汇编代写项目

Project 1

Consider the following instruction set:

ADD RD, RS add registers RD+RS, store the result in RD

SUB RD, RS subtract registers RD-RS, store the result in RD

LOAD RD,[RS] load 1 byte from memory at address RS into RD

STORE RD,[RS] store 1 byte from RD into memory at address RS

JLEZ RD,RS if RS<=0, jump to address in RD JALR RD,RS save PC+1 into RS, jump to RD LUI RT, IMM load 4-bit IMM into the upper four bits of RT LLI RT, IMM load 4-bit IMM into the lower four bits of RT There are four 8-bit registers: A, B, C, D and a program counter PC. All registers hold a signed 2's complement value. This is an encoding: A=00, B=01, C=10, D=11 ADD 0 0 0 0 RD1 RD0 RS1 RS0 example: ADD B,C = 0 0 0 0 0 1 1 0 = 06h SUB 0 0 0 1 RD1 RD0 RS1 RS0 example: SUB D,D = 0 0 0 1 1 1 1 1 = 1Fh LOAD 0 0 1 0 RD1 RD0 RS1 RS0 example: LOAD C,[B] = 0 0 1 0 1 0 0 1 = 29h STORE 0 0 1 1 RD1 RD0 RS1 RS0 example: STORE C,[B] = 0 0 1 1 1 0 0 1 = 29h JLEZ 0 1 0 0 RD1 RD0 RS1 RS0 example: JLEZ A,B = 0 1 0 0 0 0 0 1 = 41h JALR 0 1 0 1 RD1 RD0 RS1 RS0 example: JALR C,D = 0 1 0 1 1 0 1 1 = 5bh LUI 1 0 RT1 RT0 IMM3 IMM2 IMM1 IMM0 example: LUI C, 9 = 1 0 1 0 1 0 0 1 = A9h LLI 1 1 RT1 RT0 IMM3 IMM2 IMM1 IMM0 example: LLI B, 2 = 1 1 0 1 0 0 1 0 = D2h Problem 1: Write the fibonacci program you did in the homework in this assembly language and convert it to machine code. Memory address FE is the input and holds the index of the fibonacci number you're looking for. Address FF is the output. You should write your final fibonacci number here. Example: memory[FE] is 6 your program should compute 0 1 1 2 3 5 8 and put 8 in memory[FF] Hint: Keep at least two registers free for temporary operations. Instead of tying up a register as a counter, just decrement memory[FE] each iteration and stop when it reaches 0. Problem 2: Write a program that emulates this instruction set and runs your program from problem 1. You may use any language you want, but I *strongly* recommend C or C++ so you can use the unsigned char datatype. I recommend you build this emulator as follows: Step 1: Setup the variables You will need global variables to hold the PC, A, B, C, D registers and the memory. I recommend that you make them all unsigned char, a datatype that only store 8-bits, which means you don't have to worry about what happens when a program adds 250+10. Otherwise you will need to make sure that the value in all registers and memory never exceeds 255 and is never less than 0. You should make memory an array with 256 elements. Also consider making the registers an array unsigned char registers[4] rather than defining them seperately. In main() set all the registers to 0. Step 2: Load the test program In main(), use a for loop to copy this test program into your memory array, starting at 0. unsigned char program[0x16] = {0xbf,0xfe,0x23,0x15,0xa0,0xe3,0xb1,0xf0, 0x4e,0x04,0xb0,0xf1,0x1b,0xb0,0xf6,0x5f, 0xbf,0xff,0x37,0xb1,0xf3,0x5f}; Then write memory[0xfe] = 2; At the end of main, print out memory[0xff]. Once you get your emulater working correctly, it should print out 6 at the end. The test program multiplies 2 * 3. The code for it and my emulator printout is at the end of the assignment. Step 3: Read an instruction and increment PC Do this in main, before you print memory[0xff]. Make a temporary instruction variable and read from memory[PC]. Step 4: Extract the fields Make some temporary variables to hold rs, rt, rd, imm, opcode, and extract those values from your instruction variable. The & and >> operations (logical AND, bit shift-right) will be essential here. For example, since rd is bits 3 and 2 of the instruction, you would write:

unsigned char rd = (instruction >> 2) & 3;

Step 5: Write an if / else if for each instruction

Step 6: Put a debugging statement after your series of if’s

I used printf(“PC=%x, inst=%x, op=%s, A=%x, B=%x, C=%x, D=%x\n”,PC,instruction,iname,register[0],register[1],register[2],register[3]); I also make a string iname variable and set it to “ADD”, “LUI”, etc based on the instruction.

Step 7: Put everything from Steps 3-6 in a for loop

Technically your emulator should loop forever, but for debugging you’ll only want to run 10 or 20 instructions until everything looks correct.

Step 8: Debug!!!

Compare each line of your printout with my solution printout at the end of this document. Make sure register content is identical!

Step 9: Put in your fibonacci program

Then set memory[0xfe] to 6. Run it for 200-300 instructions. Do you get 8 printing out at the end?

Problem 3: Do this in Verilog

A partial implementation in Verilog is included with the assignment. Only the ADD and LUI commands are done. Can you put in the other instructions?

You can test your verilog code by downloading the compiler from

http://www.swarthmore.edu/NatSci/mzucker1/e15/iverilog-instructions.html

or in a browser at http://www.iverilog.com/index.php

When you get it working, try your fibonacci code.

Grading:

Attempted problem 1, went no further: 60 / 100 (D-)

Attempted problem 2, compiles, but doesn’t work 70 / 100 (C-)

Problem 2 complete and test program works 80 / 100 (B-)

Problem 2 complete and your fibonacci works 85 / 100 (B)

Problem 3 attempted +5 pts (90 / 100 if problem 2 is done)

Problem 3 works 100/100 A+

You get your verilog running on an actual FPGA 120/100 A+++!!!

Test program: take a number from FE, multiply it by 3, and save the output in FF

I’m using A to hold the input number, B to hold the product, and C to count down.

0 LUI D,0xf bf

1 LLI D,0xe fe

2 LOAD A,[D] 23 ;read from mem[FE]

3 SUB B,B 15 ;B=0

4 LUI C,0 a0

5 LLI C,3 e3 ;C=3

6 loop: LUI D,1 b1 ;put the memory address of done in D

7 LLI D,0 f0

8 JLEZ D,C 4e ;if C<=0, goto done 9 ADD B,A 04 ;B=B+A a LUI D,0 b0 b LLI D,1 f1 c SUB C,D 1b ;C=C-1 d LUI D,0 b0 ;put the memory address of the loop in D e LLI D,6 f6 f JALR D,D 5f 10 done: LUI D,0xf bf 11 LLI D,0xf ff 12 STORE B,[D] 37 ;save the result in mem[FF] 13 hang: LUI D,1 b1 14 LLI D,3 f3 15 JALR D,D 5f ;loop forever My solution printed out the following for the test program: PC=1, inst=bf, op=LUI, A=0, B=0, C=0, D=f0 PC=2, inst=fe, op=LLI, A=0, B=0, C=0, D=fe PC=3, inst=23, op=LOAD, A=2, B=0, C=0, D=fe PC=4, inst=15, op=SUB, A=2, B=0, C=0, D=fe PC=5, inst=a0, op=LUI, A=2, B=0, C=0, D=fe PC=6, inst=e3, op=LLI, A=2, B=0, C=3, D=fe PC=7, inst=b1, op=LUI, A=2, B=0, C=3, D=1e PC=8, inst=f0, op=LLI, A=2, B=0, C=3, D=10 PC=9, inst=4e, op=JLEZ, A=2, B=0, C=3, D=10 PC=a, inst=4, op=ADD, A=2, B=2, C=3, D=10 PC=b, inst=b0, op=LUI, A=2, B=2, C=3, D=0 PC=c, inst=f1, op=LLI, A=2, B=2, C=3, D=1 PC=d, inst=1b, op=SUB, A=2, B=2, C=2, D=1 PC=e, inst=b0, op=LUI, A=2, B=2, C=2, D=1 PC=f, inst=f6, op=LLI, A=2, B=2, C=2, D=6 PC=6, inst=5f, op=JALR, A=2, B=2, C=2, D=11 PC=7, inst=b1, op=LUI, A=2, B=2, C=2, D=11 PC=8, inst=f0, op=LLI, A=2, B=2, C=2, D=10 PC=9, inst=4e, op=JLEZ, A=2, B=2, C=2, D=10 PC=a, inst=4, op=ADD, A=2, B=4, C=2, D=10 PC=b, inst=b0, op=LUI, A=2, B=4, C=2, D=0 PC=c, inst=f1, op=LLI, A=2, B=4, C=2, D=1 PC=d, inst=1b, op=SUB, A=2, B=4, C=1, D=1 PC=e, inst=b0, op=LUI, A=2, B=4, C=1, D=1 PC=f, inst=f6, op=LLI, A=2, B=4, C=1, D=6 PC=6, inst=5f, op=JALR, A=2, B=4, C=1, D=11 PC=7, inst=b1, op=LUI, A=2, B=4, C=1, D=11 PC=8, inst=f0, op=LLI, A=2, B=4, C=1, D=10 PC=9, inst=4e, op=JLEZ, A=2, B=4, C=1, D=10 PC=a, inst=4, op=ADD, A=2, B=6, C=1, D=10 PC=b, inst=b0, op=LUI, A=2, B=6, C=1, D=0 PC=c, inst=f1, op=LLI, A=2, B=6, C=1, D=1 PC=d, inst=1b, op=SUB, A=2, B=6, C=0, D=1 PC=e, inst=b0, op=LUI, A=2, B=6, C=0, D=1 PC=f, inst=f6, op=LLI, A=2, B=6, C=0, D=6 PC=6, inst=5f, op=JALR, A=2, B=6, C=0, D=11 PC=7, inst=b1, op=LUI, A=2, B=6, C=0, D=11 PC=8, inst=f0, op=LLI, A=2, B=6, C=0, D=10 PC=10, inst=4e, op=JLEZ branching, A=2, B=6, C=0, D=10 PC=11, inst=bf, op=LUI, A=2, B=6, C=0, D=f0 PC=12, inst=ff, op=LLI, A=2, B=6, C=0, D=ff PC=13, inst=37, op=STORE, A=2, B=6, C=0, D=ff PC=14, inst=b1, op=LUI, A=2, B=6, C=0, D=1f PC=15, inst=f3, op=LLI, A=2, B=6, C=0, D=13 PC=13, inst=5f, op=JALR, A=2, B=6, C=0, D=17 PC=14, inst=b1, op=LUI, A=2, B=6, C=0, D=17 PC=15, inst=f3, op=LLI, A=2, B=6, C=0, D=13 PC=13, inst=5f, op=JALR, A=2, B=6, C=0, D=17 PC=14, inst=b1, op=LUI, A=2, B=6, C=0, D=17 PC=15, inst=f3, op=LLI, A=2, B=6, C=0, D=13 Final value: 6

发表评论

电子邮件地址不会被公开。 必填项已用*标注