Assignment4Sol

Assignment4Sol - McGill University Department of Electrical...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
McGill University Department of Electrical and Computer Engineering ECSE-221 Introduction to Computer Engineering Assignment 4 – Assembly Language Programming Due Date: Monday, November 16 th 2009 at 5:00pm Question 1 (20 points) A useful exercise in understanding assembly language and its relation to machine language is to write a short program and translate it by hand. Write a program, which we’ll call parity, that counts the number of bits set to 1 in $4 and returns 0 is even and 1 if odd $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
Background image of page 1

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

View Full DocumentRight Arrow Icon
Question 2 (20 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 (by hand) 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. ) a) x = (x < y); (5 points) Assembly: slt $4,$4,$5 #set x to 1 if x<y done: Machine Code: 000000 Opcode for slt 00100 Code for $4 00101 Code for $5 00100 Code for $4 00000 Shamt of 0 101010 Function 0x2a b) x = (x > y) || (x < z); (5 points) Assembly : slt $4,$5,$4 #set x to 1 if x>y bne $4,$0,part2 #set x to 1 slt $6,$4,$6 #set z to 1 if x<z
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 12

Assignment4Sol - McGill University Department of Electrical...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online