Unit 3. Assembly programming Exercises

Similar documents
ECE 30 Introduction to Computer Engineering

Lecture 7: Examples, MARS, Arithmetic

Run time environment of a MIPS program

Compiling Techniques

Chapter 2. Instructions: Language of the Computer. Adapted by Paulo Lopes

We will study the MIPS assembly language as an exemplar of the concept.

MIPS ISA and MIPS Assembly. CS301 Prof. Szajda

Mips Code Examples Peter Rounce

MIPS Assembly Programming

1 5. Addressing Modes COMP2611 Fall 2015 Instruction: Language of the Computer

The Program Counter. QtSPIM Register Display (Fixed-Point Registers) Erik Jonsson School of Engineering and Computer Science

MIPS Assembly Language Programming

MIPS Assembly Language Programming

Problem 3: Theoretical Questions: Complete the midterm exam taken from a previous year attached at the end of the assignment.

Compiling Code, Procedures and Stacks

Lecture 2. Instructions: Language of the Computer (Chapter 2 of the textbook)

Part 1 (70% of grade for homework 2): MIPS Programming: Simulating a simple computer

Computer Organization and Structure. Bing-Yu Chen National Taiwan University

2B 52 AB CA 3E A1 +29 A B C. CS120 Fall 2018 Final Prep and super secret quiz 9

MIPS Reference Guide

Computer Architecture

Examples of branch instructions

Computer Systems and Architecture

CS3350B Computer Architecture MIPS Instruction Representation

Lecture 5: Procedure Calls

Computer Organization MIPS ISA

CSCE 5610: Computer Architecture

Flow of Control -- Conditional branch instructions

Field 6-Bit Op Code rs Field rt Field 16-bit Immediate field

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015

Overview. Introduction to the MIPS ISA. MIPS ISA Overview. Overview (2)

MIPS Functions and the Runtime Stack

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline.

syscall takes a service ID (number) in $v0 Read integer $v0=5, after syscall, $v0 holds the integer read from keyboard

ECE331: Hardware Organization and Design

We can emit stack-machine-style code for expressions via recursion

ECE260: Fundamentals of Computer Engineering

Thomas Polzer Institut für Technische Informatik

MIPS%Assembly% E155%

Reminder: tutorials start next week!

ISA: The Hardware Software Interface

CSCI 402: Computer Architectures. Instructions: Language of the Computer (3) Fengguang Song Department of Computer & Information Science IUPUI.

2) Using the same instruction set for the TinyProc2, convert the following hex values to assembly language: x0f

ELEC / Computer Architecture and Design Fall 2013 Instruction Set Architecture (Chapter 2)

Lecture 6 Decision + Shift + I/O

MIPS R-format Instructions. Representing Instructions. Hexadecimal. R-format Example. MIPS I-format Example. MIPS I-format Instructions

COMPUTER ORGANIZATION AND DESIGN

bits 5..0 the sub-function of opcode 0, 32 for the add instruction

CS3350B Computer Architecture

Introduction to MIPS Processor

Computer Systems and Networks

CENG3420 Lecture 03 Review

Reduced Instruction Set Computer (RISC)

CS3350B Computer Architecture MIPS Introduction

Course Administration

ECE/CS 552: Introduction to Computer Architecture ASSIGNMENT #1 Due Date: At the beginning of lecture, September 22 nd, 2010

Computer Hardware Engineering

Chapter 1. Computer Abstractions and Technology. Lesson 3: Understanding Performance

MODULE 4 INSTRUCTIONS: LANGUAGE OF THE MACHINE

ECE468 Computer Organization & Architecture. MIPS Instruction Set Architecture

Procedure Calling. Procedure Calling. Register Usage. 25 September CSE2021 Computer Organization

Lecture 4: Instruction Set Architecture

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Lectures 3-4: MIPS instructions

Chapter 2. Instructions:

Chapter 2A Instructions: Language of the Computer

Telematics group University of Göttingen, Germany

CA226 Advanced Computer Architecture

Course Administration

Computer Architecture. The Language of the Machine

Quick Review. lw $t0, 4($a0) Registers x Memory. $a0 is simply another name for register 4 $t0 is another name for register (green sheet)

5/17/2012. Recap from Last Time. CSE 2021: Computer Organization. The RISC Philosophy. Levels of Programming. Stored Program Computers

Recap from Last Time. CSE 2021: Computer Organization. Levels of Programming. The RISC Philosophy 5/19/2011

Instructions: MIPS ISA. Chapter 2 Instructions: Language of the Computer 1

CS 61c: Great Ideas in Computer Architecture

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA MIPS ISA. In a CPU. (vonneumann) Processor Organization

Procedure Call Instructions

MIPS Assembly Language

Chapter 2 Instructions: Language of the Computer

Reduced Instruction Set Computer (RISC)

CSc 256 Midterm 1 Fall 2011

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA

CSE/EEE 230 Computer Organization and Assembly Language

MIPS Architecture and Assembly Language Overview

Assembler. Lecture 8 CS301

Chapter 2. Instructions: Language of the Computer. HW#1: 1.3 all, 1.4 all, 1.6.1, , , , , and Due date: one week.

Procedures and Stacks

ECE260: Fundamentals of Computer Engineering

Computer Organization and Structure. Bing-Yu Chen National Taiwan University

Instruction Set Architecture

MIPS Assembly Language Programming

Today s topics. MIPS operations and operands. MIPS arithmetic. CS/COE1541: Introduction to Computer Architecture. A Review of MIPS ISA.

MIPS PROJECT INSTRUCTION SET and FORMAT

Mark Redekopp, All rights reserved. EE 352 Unit 4. Assembly and the MARS Simulator Control Flow (Branch Instructions)

2.7 Supporting Procedures in hardware. Why procedures or functions? Procedure calls

CS 4200/5200 Computer Architecture I

Control Instructions. Computer Organization Architectures for Embedded Computing. Thursday, 26 September Summary

Control Instructions

RISC-V Assembly and Binary Notation

Transcription:

Unit 3. Assembly programming Exercises Exercise 1. Write a program, using the MIPS 32 assembly language, to calculate the sum of the first 100 numbers. The result must be stored in register $v0. Exercise 2. Modify the program of the exercise 1 in order to print the result. Exercise 3. Write a program that reads two integer numbers A and B. The program must to indicate if one of these numbers is multiple of the other one. Exercise 4. Write a program, using the MIPS32 assembly language that reads a number N and prints the following: 1 1 2 1 2 3 1 2 3 4.. 1 2 3 4 5. N Exercise 5. What is the sequence of MIPS instructions that allow implementing the following sentence in C language? (a and b are int variables) a = b + c + 100; Exercise 6. Write a program that reads two numbers. The program must print what number is the largest. Exercise 7. Write a program to read a number. The program must indicate if the number is odd or even. Exercise 8. Write a function with three arguments. In register $a0 receives the init address of a string, in register $a1 receives the ASCII code of a char, and in register $a2 receives the ASCII code of other char. The function must replace in the string any occurrence of the char stored in $a1 by the char stored in register $a2. Exercise 9. Consider the following program. Write an equivalent program, using the MIPS 32 assembly language. int array[1024]; global int function1() int z; for (i = 0; i < 1024; i++) array[i] = i; z = sum(1024); print_int(z); // variable int sum(int n) int s = 0; for (i = 0; i < n; i ++) s = s + vector[i]; return s; Exercise 10. Write a program, using the MIPS 32 assembly language, similar to the following program written in C.

void function1 () int z; int array[1024]; // local variable for (i = 0; i < 1024; i++) array[i] = i; z = sum(array, 1024); print_int(z); int sum(int array[], int n) int s = 0; for (i = 0; i < n; i ++) s = s + array[i]; return s; Exercise11. Let be a 16 bit computer, with byte addressing and a set of 60 machine instructions. The computer has 8 registers. Show the instruction format for the following hypothetical instruction ADDV R1, R2, M, where R1 and R2 are registers, and M is memory address. Exercise 12. A 32 bit computer with byte addressing, has 64 machine instructions, and 128 registers. Given the instruction SWAPM addr1, addr2 that swaps the content of the memory address addr1 and addr2. a) What is the memory space addressable by this computer? b) What is the instruction format? c) Write a program fragment, using the MIPS 32 assembly language, equivalent to the previous instruction. d) If the above instruction must occupy one word, what is the range of addresses that can be addressed by addr1 and addr2? Exercise 13. Given the following program:.data:.align 2 # align next data to 2 2 array:.space 1024 # space reserved for an array of integers of 32 bits Reply: a) What is the number of components of the previous array? b) Which are the assembly instructions needed to execute the following high level sentence: c) vector[8] = 30; d) Write a program, using the MIPS 32 assembly language, which read a number and copy this number in all components of the above array. Exercise 14. Given the following program. li $a0, 5 jal function move $a0, $v0 li $v0, 1

function: li $v0, 10 move $t0, $a0 li $t1, 0 loop : beq $t0, 0, end add $t1, $t1, $t0 sub, $t0, $t0, 1 b loop end: move $v0, $t1 jr $ra a) What is the value printed by this program (first system call in the program). b) If register $a0, used for argument passing, stores a value in one-complement, What is the range of numbers that can be stored in this register? Exercise 15. Let be a function called Vowels. This function receives as parameter the init address of a string. The function calculates the number of a in the string. When the null string is passed, the function returns -1. a) Write, using the MIPS 32 assembly language, this function. b) What are the registers used to pass the argument and receive the result. c) Given the following program:.data string:.asciiz Hello Include in the main function, the assembly instructions needed to invoke the Vowels function, and print the value returned by the function. Exercise 16. Given the following program. li $a0, 5 jal function move $a0, $v0 li $v0, 1 li $v0, 10 function: li $t0, 10 bgt $a0, $t0, et1 li $t0, 10 add $v0, $t0, $a0 b et2 et1: li $t0, 8 add $v0, $t0, $a0 et2: jr $ra

What is the value printed by this program? Exercise 17. Given a function called AddValue,with trhee parameters: The first one is the init address of an array. The second one is an integer value, which represent the number of components of the array. The third one is an integer value. This function modifies the array, adding the value passed in the third argument to all components of the array. a) What are the registers used to pass the arguments? b) Write the code of this function c) Given the following program:.data v:.word 7, 8, 3, 4, 5, 6 Include in the main function, the assembly instructions needed to invoke the function AddValue implemented in b) in order to add the value 5 to all components of the array v defined in the previous data section. Then, the program must write all components of the array v. Exercise 18. A 32-bit computer with memory addressed by bytes has 64 registers. We want to design the format for the instructions of this computer, assuming: a) The computer has 115 instructions. b) The execution model in register-register. c) The computer has the following types of instructions: Instructions for transferring data between memory and registers. These instructions use two types of addressing modes: direct, and base-register addressing Arithmetic and logic instructions. Branch instructions. There are two types of instructions: unconditional to a memory address, and conditional branches. For conditional branches, the condition is expressed in a register, and the branch is indicated with PC-relative addressing. a) Design the format for the instructions of this computer, having into account that all instructions occupy one word. b) If the displacement used in the instructions with base-register addressing uses two-complement, what is the maximum value for these displacements? c) If the address used in the unconditional branch instructions use binary code (unsigned), what is the range of addresses for the branch? d) What is the model of this computer, RISC or CISC? Exercise 19. A 16-bits computer has a memory addressed by bytes. The computer has 16 registers, and a set of 180 machine instructions. Among these instructions, we can find the following: LOADI R, imm Load in register R the immediate value imm STORE R, addr Store in memory (address addr), the value stored in register R ADDI R1, R2, imm Add the value stored in R2 to imm, and store the result in R1 ADD R1, R2, Add R1 to R2, and the result is stored in R1 BNEZ R, addr If the content of R is 0, branch to addr The addresses and immediate values are integers of 16 bits. Given the following assembly code fragment: LOADI R1, 0 LOADI R2, 3 LOADI R3, 1

ETI: ADD R1, R3 ADDI R3, R3, 2 ADDI R2, R2, -1 BNEZ R2, ETI STORE R3, 500 Answer: a) Design a possible format for the previous instructions. b) If the above program is loaded in memory in the address 1000, how many words use this program? What is the content of these memory locations? c) What is the behavior of this program? What is the content of registers R1, R2, and R3, when finish the execution of this program? Exercise 20. A 64-bit computer with a memory addressed by bytes and 256 registers, has 190 machine instructions. Consider the following instructions: LOAD Reg, addr, which stores the content of register reg in the memory address addr. STORE Reg1, (Reg2), which stores the content of register Reg1 in the memory address stored in register Reg2. BEQZ Reg1, addr, if the content of register Reg1 is zero, next instruction to execute is the instruction stored in addr. Reply: a) Describes the addressing modes using in these instructions. b) Indicate a possible instruction format for theses instructions, assuming that all instructions ocuppy one Word. c) What is the maximum value that can be encoded in LOAD and BEQZ instructions? Exercise 21. Write a program to read three numbers (A, B, C) and implement the following high level code. if (A == 2 && B == 3 C!= 4) imprimir (1); else imiprimir (2); Exercise 22. Translate to assembly the following functions. int f (int v[], int k) int v1[100]; int v2[100]; int v3[100]; if ( k > 100) return -1; for (i = 0; i < k; i++) v1[i] = i + 1: v2[i] = i + 2; v3[i] ) i + 3; for (i = 0; i < k; i++) v[i] = v1[i] + v2[i] + v3[i]; return (v[0]); Exercise 23. Translate to assembly the following functions:

inf f (int k) int v[100]; int i = 0; int s = 0; for (i = 0; i < k; i = i +2) v[i] = g(k + 2); for (i = 0; i < k; i = i + 2) s = s + v[i]; return (s); int g(int k) if (k % 2 == 0) return (k * k + 2); else return k * (-1);