{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

ass4sol - Introduction to Computer Engineering 1 Assignment...

Info icon This preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Introduction to Computer Engineering 1 Assignment 4 Assembly Language Programming Due Date: 5:00 PM Monday 15 th March 2010 Question 1 (25 points) A useful exercise in understanding assembly language and its relation to machine language is to write a short program and translate it. Write a program, which we’ll call parity, that counts the number of bits set to 1 in $4 (input) and returns 0 if that value is even and 1 if it is odd. The output should be located in $6. In writing your code, it will not be necessary to preserve context (it will be entered directly from main). You program must be relocatable , i.e., it should run from any location in memory provided that it is properly loaded on a word boundary. Next, translate your program to machine code. Although it might be tempting to simply let the SPIM assembler do this, the exercise is useful way of learning the MIPS instruction formats. Refer to the text for descriptions of MIPS assembly language instructions and the corresponding machine codes. Finally, load your program at 0x400100 and execute it. A before and after screenshot will suffice to demonstrate correct loading and functioning of your program. Note: The following method can be used to load and execute your hand-assembled machine code: .text 0x400100 # This specifies where the code is loaded .globl main main: .word 0xabcd0123 # Yes, the assembler allows you to define .word 0xabcd3210 # data in the text segment. It is under etc… # stood that this corresponds to machine # instructions. .text .globl main main: li $10,32 #set up counter li $11,0 #sum register li $4,10 #test input main10: andi $12,$4,1 #mask bit add $11,$11,$12 #sum lsb ror $4,$4,1 #check next addi $10,$10,-1 #decrement count bne $10,$10,main10 #loop till done andi $6,$11,1 #return parity main20: b main20 #hang on finish
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Question 2 (25 points) MIPS' native assembly code only has two branch instructions, beq and bne, and only one comparison instruction, slt. Using just these three instructions (along with the ori instruction to set a to 0 or 1), write the MIPS assembly language equivalents for the following "C" code snippets, and then also translate the resulting assembly instructions into machine code explaining for each line how you worked out the machine instruction. (Assume that x is stored in register $6, y is stored in register $7, and z is stored in register $8. Remember that in "C", if an expression is true, it evaluates to 1 and if false, it evaluates to 0. Refer to Appendix A of the text for descriptions of MIPS assembly language instructions and the corresponding machine codes. You are not limited to register $6,$7,$8 ) Here is an example: x = (x < y); Assembly code : slt $6,$6,$7 #sets x to 1 if x<y done: Machine Language : 0x00C7302A 000000 Opcode for slt 00110 Code for $6 00111 Code for $7 00110 Code for $6 00000 Shamt of 0 101010 Function 0x2a NOTE THE OFFSET FOR BEQ AND BNE MAY VARY BY 1 DEPENDING ON IF YOU ADD THE OFFSET TO THE BEQ/BNE INSTRUCTION OR THE NEXT ONE. BOTH ANSWERS ARE VALID a) x = (y > z) (5 points) Assembly: slt $6,$8,$7 #sets x if z<y done: Machine Language: 000000 Opcode for slt 01000 Code for $8 00111 Code for $7 00110 Code for $6 00000 Shamt of 0 101010 Function 0x2a
Image of page 2
b) x = (z != x) && (y > x) (5 points)
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern