This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: 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 well 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 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 TO THE BEQ/BNE INSTRUCTION OR THE NEXT ONE....
View Full Document
This note was uploaded on 05/06/2010 for the course ENG ECSE221 taught by Professor Ferri during the Winter '09 term at McGill.
- Winter '09
- Assembly Language