• Tidak ada hasil yang ditemukan

MIPS Assembly Language Programming using QtSpim

N/A
N/A
Nguyễn Gia Hào

Academic year: 2023

Membagikan "MIPS Assembly Language Programming using QtSpim"

Copied!
163
0
0

Teks penuh

Introduction

Additional References

The text (or code) section is where the machine language is stored (ie 1s and 0s representing the code). Address data (hexadecimal representation) Data (ASCII representation) . string of numbers from sample code) or ASCII representation (ie declared strings).

Illustration 1: Computer ArchitectureScreen / Keyboard /
Illustration 1: Computer ArchitectureScreen / Keyboard /

MIPS Architecture Overview

Architecture Overview

When a program is executed, it must be copied from disk to RAM. This is similar to storing a short term paper on the disk drive, and when you write/edit the term paper, it is copied from the disk drive to memory.

Data Types/Sizes

Memory

Based on the little-endian architecture, the LSB is stored in the lowest memory address and the MSB is stored in the highest memory location.

Memory Layout

CPU Registers

Reserved Registers

Registers $k0 and $k1 are reserved for use by the operating system and may not be used in user programs. The $at register is used by the assembler and must not be used in user programs.

Miscellaneous Registers

The $gp register is used as a pointer to global data (as needed) and should not be used in user programs.

CPU / FPU Core Configuration

Additionally, there are some basic requirements for naming a "main" procedure (ie, the first procedure to execute). Calculate the sum of the floating point doubles in the registers FRsrc1 and FRsrc2 and put it in the register FRdest.

Data Representation

Integer Representation

Two's Complement

The same process is used to encode a decimal value into two's complement and from two's complement back to decimal.

Byte Example

Halfword Example

Unsigned and Signed Addition

Floating-point Representation

IEEE 32-bit Representation

  • IEEE 32-bit Representation Examples

The numbers after the initial 1, not including the initial 1, are stored left aligned in the fractional part of the word. The result must be converted to a byte (8 bits) and stored in the biased exponent part of the word.

IEEE 64-bit Representation

Calculate the sum of the floating-point singles in registers FRsrc1 and FRsrc2 and place it in register FRdest. Calculate the quotient of the floating point double in registers FRsrc1 and FRsrc2 and place it in register FRdest.

QtSpim Program Formats

Assembly Process

Comments

Assembler Directives

Data Declarations

Integer Data Declarations

String Data Declarations

If you want to define a string with multiple lines, a NULL termination would only be required on the last or last line. When printing using the 'message' start address, anything up to (except . inclusive of) NULL will be displayed.

Floating-Point Data Declarations

As such, the declaration using multiple lines is irrelevant to the final displayed output.

Constants

Program Code

Labels

Program Template

Calculate the quotient of the floating-point singles in registers FRsrc1 and FRsrc2 and put it in register FRdest. Calculate the difference between floating point singles in registers FRsrc1 and FRsrc2 and put it in register FRdest.

Instruction Set Overview

Pseudo-Instructions vs Bare-Instructions

A pseudo-instruction is an instruction that the assembler or simulator will recognize but then convert to one or more bare instructions.

Notational Conventions

Hexadecimal or base-16 immediate values ​​can be used, but must be prefixed with a 0x to indicate the value is hex.

Data Movement

Load and Store

For the halfword and byte instructions, only the lower 16 bits or the lower 8 bits are used.

Move

The floating point section will include examples for moving data between integer and floating point registers.

Integer Arithmetic Operations

Example Program, Integer Arithmetic

Refer to the system utilities section for information on displaying final results on the console.

Logical Operations

Shift Operations

  • Logical Shift
  • Arithmetic Shift
  • Shift Operations, Examples

Each bit in the source operand is shifted the specified number of bit positions, and the newly freed bit positions are filled with zeros. Each bit in the source operand is shifted the specified number of bit positions, and the new free bit positions to the left are filled.

Control Instructions

Unconditional Control Instructions

Conditional Control Instructions

Conditional control instructions include the standard set; equal branch, unequal branch, less than branch, less than or equal branch, greater than branch, and greater than or equal branch. In addition, these conditional control instructions can be modified by adding or appending a 'z' at the end which means a comparison to zero (0) without typing the immediate 0 in the instruction.

Example Program, Sum of Squares

Floating-Point Instructions

Floating-Point Register Usage

Floating-Point Data Movement

The two double-precision operands (l.d and mov.d) reference registers $f6 and $f8, but use registers $f6/$f7 and $f8/$f9 to hold each of the two 64-bit values.

Integer / Floating-Point Register Data Movement

For example, assuming an integer value is in integer register $s0, to copy the value to floating point register $f12, the following instruction could be used. To copy the contents of $f12, into an integer register $t1, the following instruction could be used.

Integer / Floating-Point Conversion Instructions

In this example, the integer value in $s0 copied to $f12 still appears as a two's complement integer. This code snippet loads the integer value into variable iNum into $t0 and then copies the value into $f6.

Floating-Point Arithmetic Operations

Example Programs

  • Example Program, Floating-Point Arithmetic
  • Example Program, Integer / Floating-Point Conversion

A pop copies the top of the stack to the operand and then adds 4 bytes (in that order). Calculate the difference of the floating-point doubles in registers FRsrc1 and FRsrc2 and put it in register FRdest sub.s FRdest, FRsrc1,.

Addressing Modes

Direct Mode

Immediate Mode

Indirection

Bounds Checking

If the assembly language program attempts to access the 110th element of an array, the value in that memory location is returned without errors. In addition, no error is thrown when a program attempts to access a word (32-bit) in a half-word (16-bit) array.

Examples

Example Program, Sum and Average

If the memory access being attempted is outside the general scope of the program, an exception will be thrown. For example, attempting to access a memory location in a reserved section would not be allowed and would thus throw an exception.

Example Program, Median

Put the logical AND of the integers from register Rsrc1 and Src2 (or Imm) into register Rdest. FRsrc2 Floating-point Multiply, double - Calculate the product of the floating-point doubles in registers FRsrc1 and FRsrc2 and put it in register FRdest mul.s FRdest, FRsrc1,.

Stack

Stack Example

Since the stack is last-in, first-out, the first item to pop off the stack is the last item to be pushed, or 37 in this example.

Stack Implementation

Push

Pop

Multiple push's/pop's

Example Program, Stack Usage

Place the logical NOR of the integers from register Rsrc1 and Src2 into register Rdest. FRsrc2 Floating-point Multiply, Single - Compute the product of the floating-point units in the FRsrc1 and FRsrc2 registers and place it in the FRdest neg.d FRdest register, FRsrc Negate, Double.

Procedures/Functions

MIPS Calling Conventions

The standard calling convention specifies actions for the caller (routine calling) and the callee (routine being called).

Procedure/Function Format

Caller Conventions

On completion of the procedure, the caller procedure should restore all stored unsaved registers and adjust the stack point ($sp) if necessary if arguments have been passed on the stack.

Linkage

Argument Transmission

Call-by-Value

Call-by-Reference

Argument Transmission Conventions

Function Results

Registers Preservation Conventions

Miscellaneous Register Usage

Summary, Callee Conventions

Call Frame

Stack Dynamic Local Variables

Procedure Examples

Example Program, Power Function

Example program, Summation Function

Example Program, Pythagorean Theorem Procedure

Place logical OR of integers from register Rsrc1 and Imm to register Rdest role Rdest, Rsrc1, Src2 Rotate left. Place the logical XOR of the integers from the Rsrc1 register and Imm into the Rdest register.

QtSpim System Service Calls

Supported QtSpim System Services

QtSpim System Services Examples

Example Program, Display String and Integer

The console window can be shown or hidden in the Windows menu (on the top bar).

Example Program, Display Array

Example Program, Read Integer

Note, the default console window will typically be larger than the one shown above.

Example Program, Read String

Logical OR of integers from register Rsrc1 and Src2 to register Rdest or Rdest, Rsrc1, Imm OR Immediate. Put logical XOR of integers from register Rsrc1 and Src2 into register Rdest output Rdest, Rsrc1, Imm XOR Immediate.

Multi-dimension Array Implementation

High-Level Language View

It is expected that the reader is generally familiar with the use of the high-level language of two-dimensional arrays.

Row-Major

Column-Major

Example Program, Matrix Diagonal Summation

Branch conditionally to the instruction at the label if the content of Rsrc is greater than 0. Branch conditionally to the instruction at the label if the content of Rsrc is less than 0.

Recursion

Recursion Example, Factorial

Example Program, Recursive Factorial Function

Recursive Factorial Function Call Tree

Arguments and temporary values ​​for each instance are different since they are held on the stack as required by the standard calling convention. It should also be noted that the height of the recursion tree is directly related to the amount of stack memory used by the function.

Recursion Example, Fibonacci

Example Program, Recursive Fibonacci Function

Recursive Fibonacci Function Call Tree

Convert the single-precision floating-point number in the FRsrc register to a double-precision number and place it in the FRdest register. Convert the double-precision floating-point number in the FRsrc register to a single-precision number and place it in the FRdest register.

Appendix A – Example Program

Appendix B – QtSpim Tutorial

Downloading and Installing QtSpim

QtSpim Download URLs

Installing QtSpim

Working Directory

Sample Program

QtSpim – Loading and Executing Programs

  • Starting QtSpim
  • Main Screen
  • Load Program
  • Data Window
  • Program Execution
  • Log File
  • Making Updates

However, it is usually easier to select the Reinitialize and load icon from the main screen (second file icon in the top left). However, it is usually easier to select the Save Log File icon from the main screen.

Debugging

Convert the single precision floating point number in the FRsrc register to an integer and put it in the FRdest register div.d FRdest, FRsrc1,. Move double floating point from register FRsrc to register FRdest mov.s FRdest, FRsrc Move floating point, single.

Appendix C – MIPS Instruction Set

Arithmetic Instructions

Set $lo = Rsrc / Src (or imm) Remainder is set to $hi divu Rsrc1, Rsrc2 Divide (no overflow). Set $lo = Rsrc / Src (or imm) Remainder is set to $hi div Rdest, Rsrc, Src Divide (with overflow).

Comparison Instructions

Set register Rdest to 1 if register Rsrc1 is less than Src2 (or Imm) and to 0 otherwise. Set register Rdest to 1 if register Rsrc1 is not equal to Src2 and to 0 otherwise.

Branch and Jump Instructions

Conditionally branch to the statement on the label if the content of Rsrc equals 0 bge Rsrc1, Src2, label Branch on Greater Than or Equal. Conditionally branch to the statement on the label if the contents of Rsrc are greater than or equal to 0.

Load Instructions

Load the left bytes from the word at the possibly outstanding address into the Rdest register. Load the right bytes from the word at the address, which may be outstanding, into the Rdest register.

Logical Instructions

Rotate the contents of the Rsrc1 register to the left by the distance indicated by Src2 and put the result in the Rdest register. Shift the contents of the Rsrc1 register to the left by the distance indicated by Src2 and put the result in the Rdest register.

Store Instructions

Store the low halfword from the Rsrc register at the possible address of the unaligned usw Rsrc, Unaligned Store Word address.

Data Movement Instructions

Shift the contents of CPU integer registers Rsrc and Rsrc+1 into coprocessor 1 float registers Frdest and FRdest+1.

Floating-Point Instructions

Compare the floating-point double in the FRsrc1 register with the one in FRsrc2 and set the condition flag true if the first is less than the second. Convert the double precision floating point number in the FRsrc register to an integer and put it in the FRdest register cvt.w.s FRdest, FRsrc Convert single to integer.

Exception and Trap Handling Instructions

For additional information and a more complete list of ASCII codes (including extended ASCII characters), refer to http://www.asciitable.com/.

Appendix D – ASCII Table

Alphabetical Index

Representation

Representation

Gambar

Illustration 1: Computer ArchitectureScreen / Keyboard /

Referensi

Garis besar

Dokumen terkait

Segala puji dan syukur kehadirat Allah SWT atas berkah, rahmat dan hidayah-Nya yang senantiasa dilimpahkan kepada penulis, sehingga bisa menyelesaikan skripsi