Introduction to MIPS Assembly Language
Change of Schedule
Lecture Time
Copyright By PowCoder代写 加微信 powcoder
Course Overview
Number system and Computer Arithmetic
MIPS architecture and MIPS Assembly Language
MIPS Assembly Language
MIPS Assembly Language
Digital Logic Basics (1)
Digital Logic Basics (2)
Pre-Exam (Midterm Exam), cover Lec 1 – Lec 7
Finite State Machine and Design of Sequential Circuits
Introduction to Memory and Cache
Internal Memory and External Memory
External Memory
I/O Modules
I/O Modules and Course Summary
Review of Previous Lecture
◆General introduction to Assembly Language
– distinctions among high-level language, assembly language, machine language
– the role of assembly language
Review of Previous Lecture ◆The role of assembly language
– a symbolic human-understandable language
– the symbolic representation of machine language
– assembly language — assembler — machine language
– two specific roles: (1) high-level language — complier — assembly language; (2) use assembly language for programming directly
Review of Previous Lecture
◆Assembly language is machine-specific
– different architectures support different assembly languages (instruction set)
– MIPS is one of many architectures
– the assembly language for MIPS is one of many languages
Review of Previous Lecture ◆Brief introduction to MIPS
– register-based architecture
– Reduced Instruction Set Computer (RISC)
– three key points: (1) registers (2) memory (3) instructions
Review of Previous Lecture ◆Registers
– 32 general-purpose registers, each having 32 bits
– naming: numbers or specific names
– some are reserved, some are used by user programmers – other registers: Hi and Lo, etc.
Review of Previous Lecture ◆Memory and Address
– byte, half-word, word, double-word
– each byte has a 32-bit long address
– contiguous bytes and the address
– the address of an instruction is always the multiply of 4
Review of Previous Lecture ◆Instructions
– understand the usage of some basic instructions
– the translation between instructions and machine code
Outline of Today’s Lecture ◆Bitwise Logic Operations
– OR, AND, XOR, etc. – Shift operations
◆Arithmetic Operations and Memory Access – Add, sub, mul, div
– What are the differences for signed and unsigned numbers
Hello, Assembly World — Example of Add
## Program to add two plus three
tell the assembler what to do (.text, .globl main are called directive)
.globl main
ori $8,$0,0×2
ori $9,$0,0×3
addu $10,$8,$9
# put two’s comp. two into register 8
# put two’s comp. three into register 9
# add register 8 and 9, put result in 10
## End of file
.text: the following lines are the source code for the program
each line is an instruction
.globl main, the identifier “main” will be used outside of this source file (i.e., used globally) as the label of a particular location in main memory; it defines a symbolic address, which is a name in the source code referring to a location in the memory. In this example, “main” stands for the address of the first machine instruction (0x00400000)
Bitwise Logic Operations
◆Essentially, manipulation of bit patterns
– In the view of ALU (computer), the operands are bit patterns, each instruction conduct some manipulations of the bit patterns
– There is no concept of “numbers” for ALU
– Programmers (human) enforce the concept of numbers on bit patterns, then the manipulations could have some specific meanings
– The subtle difference between the views of ALU and Programmers are very important
Bitwise Operations with Immediate Operands
◆There is an immediate operands within the instruction
– Example: ori $8, $0, 0x2; ori: or immediate operand
0x2 is within the machine code of this instruction!
Zero Extension
◆Ori will do zero extension to the immediate
operand – padding with zeros on the left – Example: ori $8, $0, 0x2
◆For programmers: load a const into register $8
– Const: positive or negative? Unsigned or 2’s complement?
Zero Extension
◆Const: can only be non-negative integers (unsigned representation) – Why? Zero Extension
◆What if I use 16-bit 2’s complement to represent the immediate operand?
– For a negative number (start with 1), after zero extension, it becomes positive – meaningless (we are not loading that number to the register anymore!)
Zero Extension
◆“Const: can only be non-negative integers”
– This restriction is not imposed by ALU!
– This is the consequence of the operation of ALU (zero extension) to make “load” meaningful
Meaning of ORI Instruction
◆ALU: zero extend a first bit pattern, then do bitwise OR with a second bit pattern
◆Programmers: we can use ORI to implement the load function! (however, we can only load non- negative integers)
When Learning Instructions
◆Know what operations the AUL will do
◆As programmers, we can interpret the operations in different ways to implement certain functions (however, with restrictions)
◆Example: Bitwise OR – Load (just one way)
Other Bitwise Logic Operations ◆Andi d, s, const
– Zero extend a first bit pattern (const), do AND operation with a second bit pattern (s), store the result in d
◆Consider addi $8, $0, 0x2
– As a programmer, can you interpret the meaning of this operation?
Other Bitwise Logic Operations ◆Consider addi $8, $0, 0x2
– As a programmer, can you interpret the meaning of this operation?
– Clearing a register ($8) – set all bits to 0
– 0x2 should be unsigned or 2’s complement? Doesn’t matter!
Other Bitwise Logic Operations ◆Exclusive OR: xori d, s, const
– Similar to ori, andi
– You can interpret xori in different ways (the art of programming)
Other Bitwise Logic Operations
◆The other type: bitwise operation between two registers
– Example: or d, s, t
◆Again, unsigned or 2’s complement for s, t?
– It’s up to you! Depending on what functions you want to implement
– For ALU, just do bitwise operations between two bit patterns
Let’s See Some Common Functions ◆NOT (flip) as NOR with $0
– Efficient and often used
◆Question: consider or $9, $8, $0 – What’s the function?
Let’s See Some Common Functions ◆Question: consider or $9, $8, $0
– What’s the function? ◆Move as OR with $0
– Move the bit pattern in $8 to $9
◆These are some commonly used programming techniques
Shift Operations
◆Shift Left Logical: sll d, s, shft
– Shift left by shft positions
– The right-most bits are replaced by 0’s and left-most bits are discarded
Question: what’s the meaning of shifting (logical) a bit pattern left by one position?
0110 -> 1100
Shift Operations
◆Is Shift Left Logical always equal to multiplication?
– Counter example: 1100 -> 1000 (a 1 bit is discarded) – Counter example: 2’s complement 0100 -> 1000
◆So again, as programmers, there are restrictions on using Shift Left Logical to implement Multiplication
– Unsigned representation – No 1 bit is discarded
In Place Shift
◆Is it OK to do sll $8, $8, 2?
– Yes, ALU will do in place shift: copy data from register to ALU, do shift, store result back to register
◆A special instruction: sll $0, $0, 0
– For any instruction that attempts to change the content of $0,
ALU will do nothing (No-Op)
– No-Op is actually useful (sll $0, $0, 0 is the commonly used one)
Shift Right Logical ◆Instruction: srl d, s, shft
◆Equivalent to division? Again, some restrictions
– Unsigned representation or non-negative integers in 2’s
complement
– Consider 0001 -> 0000 (a rounding issue)
Shift Right Arithmetic ◆Instruction: sra d, s, shft
◆Equivalent to division?
– For negative integers in 2’s complement forms
◆Bitwise Logical Operations
– Ori, andi, and, nor, sll, …
– The manipulation of bit patterns (ALU does not view them
as numbers)
– It happens to be that the bit manipulation operations
have some specific mathematical meanings
– Programmers can view the bit patterns as numbers to achieve some functions
Integer Arithmetic
◆Integers in MIPS can be represented as unsigned
form or 2’s complement form (decided by
programmers!)
– use 32 bits to represent integers
– the algorithms to add two integers in these two forms are the same — it is a feature of 2’s complement form
– as a result, the instructions are the same — special for add – the difference lies in the overflow detection mechanism
Overflow Detection ◆Unsigned form
– overflow: the carry out bit of the highest column is 1 – e.g., 1010 + 1100 = 1 0110
◆2’s complement form
– recall: there is an overflow if both operands have the
same sign but the result has the opposite sign
+0101 0101
10000 0000 is there an overflow?
Yes — if unsigned form
No — if 2’s complement form
Instruction addu and add ◆addu d, s, t
-s+t->d (s,t,dallareregisters)
– meaning of “u”: overflow is ignored; note: it does not
mean “unsigned” ◆addd,s,t
– exactly the same as addu: s+t -> d
– difference: detect overflow and will cause an interrupt
Instruction addu and add ◆So, use addu or add?
– most assembly language programmers deal with overflow by making sure that the operands won’t cause it – the responsibility of the programmers
– usually, they will use addu
Instruction addiu ◆addiu: add immediate
– immediate value: recall I-type instructions – addiu d, s, const
– s + const -> d; const has 16 bits
◆Again, we have the problem: add s (32 bits) and const (16 bits)
– recall, we have zero-extension for bit-wise OR, ori
– sign-extension: copy the 15th bit (most significant bit) – 1111 1111 1111 1111 1010 1101 1110 1101
Subtraction Instructions
◆subu/sub in constrast to addu/add
– subu/sub d, s, t – s – t -> d
◆there are no subi/subiu
– instead addi/addiu is used
– example: if you want to subtract 3, use addiu $8, $10, -3
Example — Fill in the Blanks
ori : bit-wise OR (used to load data) sll: shift left (why is it equivalent to x4)
Example — Fill in the Blanks
ori : bit-wise OR (used to load data) sll: shift left (why is it equivalent to x4)
Multiplication Instructions ◆How many bits do we need
– check decimal: 99 x 99 = 9801
– generally: the product of two integers expressed with N-bit
binary may need 2N bits
– that is, we now need two registers to store the result
◆Hi and Lo registers
bit32-63 bit0-31
Instructions mult and multu ◆mults,t /multus,t
– automatically stores the result in hi and lo
– difference: mult for 2’s complement form; multu for unsigned
– note the difference from add and addu (whether to check overflow)
– both mult and multu would not check for overflow (but overflow could happen — again, it’s the programmer’s responsibility; blame the programmers)
How to Access the Multiplication Result? ◆Result (two parts) is stored in hi and lo
– two specific instructions:
– mfhi d # move from hi to register d
– mflo d # move from lo to register d
– note: no other instructions can access hi and lo
– you have to move the result from hi and lo to registers first
The Same Example
The Same Example
9$8 lo 99 999
why do we only use mflo, not mfhi?
The Same Example
9$8 lo 99 999
why do we only use mflo, not mfhi?
the operands are small, mfhi is filled with zeros
Division Instructions: div and divu
◆With N-bit integer division, we have two results of N-
– op1 / op2: op1 = op2 x quotient + remainder
– again, we need hi and lo to store the results
◆divs,tanddivus,t
– divide s by t; div for 2’s complement, divu for unsigned
Memory Access
◆Memory access: how to copy data from memory into registers, and also, from registers to memory
– all operations are conducted over registers — need to move operands in registers first
– load: memory -> register – store: register -> memory
Recall: Memory Address
◆Basic unit is byte — each byte has an address (32bits)
– memory is access in contiguous bytes: e.g., word (four bytes) – we use the address of the lowest-byte to refer to the contiguous
– alignment restriction: the address of a word is always a multiple of four
– an instruction is one-word long — the address of an instruction is always word aligned
Recall: Memory Address ◆Two instructions: lw and sw
– use a base register and offset to calculate address
– lw d, offset (b)
– meaning: b is a register storing the base address; offset is a 16-bit number in 2’s complement form; address = base address + offset; load the content in address to register d
– similarly, we have sw t, offset (b)
How to Set the Base Address ◆Let’s see the logic first
– we need to load data in memory into registers — we need address (32 bits) of the memory
– the essential constraint: we cannot directly refer to address in one instruction (why?)
– instead, address = base address (32 bits) + offset (16 bits const)
– set base address: we need two 16-bit immediate values
– as a result: two instructions for setting a base address, each involves one immediate value
How to Set the Base Address
◆Combination of two instructions lui and ori
– lui d, const # copy const to the upper two bytes (16 bits) of the register d (the lower two bytes are 0)
– ori d, s, imm # if we use $0, equivalently, we set the lower two bytes of d as imm (why lower two bytes? — zero extension)
– as a result: d stores the desired base address
How to Set the Base Address
◆Isn’t it complex if setting the base address whenever loading or storing data?
– note: address = base address + offset
– once base address is set, we can vary the offset to access memory
Symbolic Address
◆It would be nice if we can use symbols to represent addresses in memory
– much better readability — symbolic language
– fortunately, MIPS supports this feature
– we can use names (symbols) to refer to memory locations – e.g., x, y, z, boy, age, …
– these names are called symbolic addresses
Symbolic Address
We now encountered more directives:
.data: indicate the start of the data section of memory
.word: put a 32-bit two’s complement integer here; example .word 17
The data section in MIPS starts at address 0x10000000. As a result, the symbolic address x = 0x10000000 Question, what’s the symbolic address poly?
Symbolic Address
The data section in MIPS starts at address 0x10000000. As a result, the symbolic address x = 0x10000000 symbolic address poly = 0x10000004
(each byte has an address, a word = 4 bytes)
address 0x10000000 0x10000004 0x10000008
content 00000011 (hex) 00000000
equivalently, we have defined two values at two memory locations: value 17 at x (0x10000000), value 0 at poly (0x10000004)
That’s all about today’s lecture
◆In today’s lecture, we only covered a very small portion of MIPS assembly language
– some extra materials in Lab session – use lab session to practice
– practice more offline
– a good online source: https://chortle.ccsu.edu/AssemblyTutorial/index.html#part7
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com