代写代考 Introduction to Assembly Language

Introduction to Assembly Language

Outline of Today’s Lecture
◆General introduction to Assembly Language

Copyright By PowCoder代写 加微信 powcoder

– distinctions among high-level language, assembly language, machine language
– the role of assembly language ◆MIPS as a learning example
– MIPS architecture
– Overview of MIPS assembly language with typical examples

What Programming Language Do You Use?
High-level language (C, C++, Java)
Experienced programmer will use 0/1

Languages for Humans and Computer ◆Symbolic languages for humans
– people read and write symbols (words) much better than long sequences of digits
◆Digits for computers
– binary encoding is natural and efficient for computer
◆The Gap? – Use Assembly Language to close this gap

Start with Some Basic Concepts
◆Processor family, high-level programming language, assembly language, machine language (machine code)

Family of Processors
◆Processors can be categorized by architecture
– A family of processors share the same architecture
– Intel (i7, i5, …), AMD (Ryzen 7, Ryzen 5, …), ARM, MISP, …
◆Each processor family has a specific language – Assembly Language
– Assembly language is hardware-dependent

Assembly Language
◆One Processor Family, One Assembly Language
– One instruction of assembly language corresponds to one basic operation of hardware in the processor
Assembly Language 1
Assembly Language 2
Assembly Language 3
Processor 1
Processor 2
Processor 3

High-Level Programming Language
◆High-level programming language is independent of processors
– C++/C, Python – there’s no difference for programmers
Translator 1 Translator 2
Translator 3
Assembly Language 1
Assembly Language 2
Assembly Language 3
Processor 1
Processor 2
Processor 3
High-level Programming Language

Machine Language vs. Assembly Language ◆Machine Language
– binary representation (encoding) used for communication within a computer system
◆Assembly Language
– symbolic representation of machine language – symbols – easily understandable by humans

Machine Language vs. Assembly Language
Translator
understandable by humans (to some extent)
better for machines

Assembler as The Translator
source file
(contain the assembly language or other high-level language)
object file
(contain the machine instructions, and bookkeeping information that helps combine several objective files into a program)

Producing an Executable File
The process that produce an executable file
A program often contains several modules;
A module often contains references to other modules (to use data or subroutines);
A linker: combine independently assembled object files and resolve references

Example – The Same Program in Different Languages
C (high-level language)
Assembly language (easier to understand)
Since we already have high-level languages, why do we still need assembly language?
Machine language

When is the Assembly Language Used? ◆The Roles of assembly language
– first, high-level language will be transformed to assembly language (in some cases) using a compiler
high-level language
machine language (binary) assembly language
– second, directly used as a language to write programs and it has its unique advantages

The Two Roles of Assembly Language
Assembly language could be used to directly write a program, or as the output of the compiler

Advantages of Assembly Language ◆Main advantages
– speed, not requiring a compiler
– can exploit some hardware features that are not available
for high-level languages
– when to use it? when there are critical requirements for speed, size, performance, …
– example: embedded systems; design a control unit for car brake

Drawbacks of Assembly Language ◆Main drawbacks
– machine-specific: must be totally rewritten to run on another computer architecture
– longer than high-level languages – time consuming and more bugs
– harder to write

What we have learned so far – assembly language in general
next: MIPS – an architecture and a specific assembly language

Instruction Set Architecture (ISA)
◆A set of instructions supported by processor
– Computers can be divided into two types based on instruction set: Reduced Instruction Set Computer (RISC) vs. Complex Instruction Set Computer (CISC)
– They are two different philosophies to design computers

RISC vs. CISC
◆Instruction set contains simple or complex instructions
– RISC: a small set of simple instructions; build more complex functions based on this set; example: {+,-, X, /}
– CISC: one instruction could correspond to very complex operation; take more than one CPU cycles
– RISC example: MIPS, ARM
– For your interests: https://cs.stanford.edu/people/eroberts/courses/soco/projects/risc/risccisc/ (comparison between RISC and CISC )

MIPS – refer to a processor and assembly language ◆Why MIPS?
– used to be very popular – one in three RISC chips are MIPS-based
– still in use today – routers, embedded systems, video game consoles
– the perfect real-world implementation for learning RISC (others like ARM have many extra instructions; what you have learned on MIPS is 100% useful for learning others)

MIPS (Microprocessor without Interlocked Pipeline Stages)
◆Register-based architecture
– CPU performs operations on registers
– operations can be divided into two categories:
– (1) memory access (load and store data between memory and registers)
– (2) ALU operations (operations between registers)

MIPS (Microprocessor without Interlocked Pipeline Stages)
◆Key points
– registers
– main memory (know the addressing to access) – instructions
– MIPS assembly language – how to do operations between registers (and memory) using instructions to achieve certain functionalities

MIPS Registers
◆32 general-purpose registers
– each has 32 bits
– some are reserved for special purposes
◆Naming: how to refer to these registers
– start with “$”
– directly use register number: $0 through $31 (decimal)
– they also have names: $t1, $sp (better to use names in assembly language)

MIPS Registers
some reserved registers
Registers number 2 – 25 can be used by programmers in general
10 Temporary registers ( $t0 – $t9)
8 Saved registers ( $s0 – $s7): must save the original value in the register and restore it when not using it

Other MIPS Registers ◆Special registers
– e.g., PC
– programmer cannot access with most instructions
◆Hi and Lo registers
– for multiplication and division
– e.g., with 32 bits numbers, multiplication and division result in 64 bits numbers
– not directly addressable; must use special instructions to access (mfhi — move from Hi, mflo — move from lo)

MIPS Memory Layout — How is memory organized
◆Only a portion of the memory can be used by user program
– the top from 0x80000000 to 0xFFFFFFFF is for Operating System and ROM
◆The part that can be used for user program is divided to three segments
– Text, Data, and Stack Segment

MIPS Memory Layout — How is memory organized ◆Text segment
– store the machine language of the user program (the text)
◆Data segment: stores the data that the program operates on
– static data: size of the data is known and would not change when allocated by assembler
– dynamic data: allocated and deallocated as the program executes

MIPS Memory Layout — How is memory organized
◆Stack segment: as procedures are
activated and deactivated, variables and
parameters are pushed and popped in
– note: stack segment grows downward and dynamic segment grows upward

MIPS Memory and Address
Some basic facts:
the basic “unit” in memory is byte (8 bits); there are a total of 2^{32} bytes in memory;
each byte has an address of 32 bits (why 32);
address range: 0x00000000 to 0xFFFFFFFF (note: 0x indicates hexadecimal number)

MIPS Memory and Address
Operations:
load: copy a bit pattern (could be data or instruction) in a designated address in memory to a register (memory -> register)
store: copy a bit pattern from register to memory at a designated address (register -> memory)

MIPS Memory and Address
Memory is often accessed in Contiguous bytes (a group of bytes)
one byte = 8 bits
one word = 4 bytes, 32 bits double word = 8 bytes, 64 bits
Memory is used to store instructions and data
instruction has a fixed length of one word
data can have various bytes
Note: for contiguous bytes (a group of bytes), we often use the address of the first byte (i.e., the byte with the lowest address) — more details on this later

MIPS Instructions
◆There are 3 types of instructions in MIPS
– R-type instructions
– I-type instructions
– J-type instructions
– each instruction has 32 bits

R-type Instructions ◆Format
– three arguments: two source registers (rt and rs) and one destination register (rd)
– written as instruction rd, rs, rt
– example: add $t0, $t1, $t2 (meaning?)
– meaning: add the values in $t1 and $t2 and stores the result in $t0

R-type Instructions ◆The machine code
– “instruction rd, rs, rt” is a line of code in assembly language
– need to translate it to machine language (binary) — assembler
– the machine code has fixed format (32 bits)
6 bits 5 bits 5 bits
5 bits 5 bits 6 bits

R-type Instructions
6 bits 5 bits 5 bits
5 bits 5 bits 6 bits
op or opcode: “operation code”; indicate the type of an instruction; all R-type instructions have opcode 000000
rs, rt, rd: indicate which register; question: why do we use 5 bits?
there are 32 general-purpose registers; example: rd = 01000 -> register $8 = $t0
shamt: used in shift instruction; how many positions (bits) to shift funct: indicate which instruction; e.g., 100000 -> add

R-type Instructions – Example
5 bits 5 bits
assembly language:
add $t0, $ t1, $t2
machine language: op rs 000000 01001
shamt 00000
funct 100000
01000 $8 ($t0)

R-type Instructions – Example
6 bits 5 bits
5 bits 5 bits
5 bits 6 bits
assembly language: sll $t5,
machine language: op 000000
shift the value in $t4 two bits to the left and place the result in $t5
rs rt 00000 01100
01101 $13 ($t5)
shamt 00010
funct 00000

R-type Class Exercise
◆Write a line of code to implement 3+10
– suppose 3 and 10 are already loaded in $t3, $t5 – store the result in $t1
◆Assembling: translate the code into machine
6 bits 5 bits 5 bits
5 bits 5 bits 6 bits

I-type Instructions ◆Format
– three arguments: two registers (rt and rs) and a 16-bit “immediate” value
– immediate value: store the value directly within the instruction
– immediate value: could be a constant serving as one operand in the arithmetic operation; or an address in load/store instructions
– assembly language: instruction rs, rt, imm (but not always)

I-type Instructions
◆Machine code
– op: specify which instruction (different from R-type)
6 bits 5 bits 5 bits
Constant/address

I-type Instructions
◆Example: bit-wise OR operation (ori)
– assembly language: ori rt, rs, imm
– operation: take the bit-wise OR between the value
stored in rs and imm, then store the result in rt – example: ori $8, $0, 0x2
– what is $0?
– what is 0x2 —> we can write it in this simple form in assembly language

I-type Instructions
◆Is there any problem for the bit-wise OR?
– value in $0 has 32 bits, imm has 16 bits; how can we do bit-wise OR
– zero extension: MIPS will zero-extend the 16-bit operand imm to 32 bits, by padding with zeros on the left
– 0000 0000 0000 0000 0000 0000 0000 0010 (0x2)
– 0000 0000 0000 0000 0000 0000 0000 0000 (value in $0) – 0000 0000 0000 0000 0000 0000 0000 0010 (put in $8)

I-type Instructions
◆Machine code of ori $0, $8, 0x2
ori 001101
0000 0000 0000 0010
Constant/address

I-type Instructions
◆What’s the effect of ori (bit-wise OR)? – ori $8, $0, 0x2

I-type Instructions
◆What’s the effect of ori (bit-wise OR)?
– load the value stored in imm to the target register, by using the constant value in $0;
◆Class exercise
– use ori to load decimal 17 into register $t1

I-type Instructions
◆Another Example: load word (lw)
– operation: load a word from memory at a designated address to a register
– the address of that memory has 32 bits
– the length of instruction is 32-bit
– problem: we need to specify the memory address (32 bits) in the instruction (32 bits); how can we do that

I-type Instructions
◆Another Example: load word (lw)
– operation: load a word from memory at a designated address to a register
– the address of that memory has 32 bits
– the length of instruction is 32-bit
– problem: we need to specify the memory address (32 bits) in the instruction (32 bits); how can we do that
– solution: base address + offset

I-type Instructions ◆Example: load word (lw)
– assembly language: lw, destreg, offset (basereg)
– destreg: name of the register to put data in
– offset: a 16-bit signed constant (immediate value) – basereg: the register containing the base address – memory address = base address + offset

I-type Instructions ◆Example: lw $8, 0x60 ($10)
– suppose $10 stores the value 0x00400000 (base address) – memory address = 0x00400060
– load the word in the memory (0x00400060) to $8
◆Machine code
– 100011 01010 01000 0000 0000 0110 0000 – lw (op) $10 (base) $8 (dest) 0x60 (offset)
– lw $8, 0x60($10)

I-type Instructions ◆Example: store word (sw)
– assembly language: sw, t, offset (basereg)
– operation: copy the data stored in register t to the
memory at address ( base address in basereg + offset)
– example: sw $12, 0x50 ($13)

I-type Instructions
◆Other data transfer instructions (not exhaustive)
ld destreg, const(addrreg)
load/store double word, destreg must be even numbered registers
sd destreg, const(addrreg)
lh destreg, const(addrreg)
load/store half word, sign is extended to width of register
sh destreg, const(addrreg)
lhu destreg, const(addrreg)
load/store half word, no sign extension
shu destreg, const(addrreg)
lb destreg, const(addrreg)
load/store byte, sign is extended to width of register
sb destreg, const(addrreg)
lbu destreg, const(addrreg)
load/store byte, no sign extension
sbu destreg, const(addrreg)

J-type Instructions
◆Function: control the program flow to a given instruction
◆Format: instruction addr
◆Typical example: j addr (jump to the instruction
at address addr) ◆Machine code:

Jump Instruction
◆How to jump to an instruction at addr — change the content of PC
◆Let’s first see the effect of jump instruction
PC stores the address of the next instruction
no-operation
after the jump, PC changes to 00400000— a loop function

Jump Instruction Exercise: fill in the blanks

Jump Instruction Exercise: fill in the blanks

Jump Instruction
◆Jump: j addr
◆Again, we have a problem: addr has 26 bits, the
address of an instruction has 32 bits ◆How is this solved in MIPS? — we need a
mechanism to transform the 26-bit address in the instruction to a 32-bit address

Jump Instruction
◆The address of instructions
– one instruction occupies 4 bytes in memory (recall that each byte in memory has an address)
– recall: we use the address of the lowest byte to refer to contiguous bytes
– restriction: the address of the lowest byte should be a multiple of 4
– as a result, the right-most two bits in the address of an instruction are always 00 — we still need 30 bits

Jump Instruction
◆The address of instructions
– the left-most 4 bits in the address
– they are set as the left-most 4 bits of the content in PC – why? do not jump too far away

Jump Instruction Example
machine code: op addr (26bits)
the target address we want to jump to (how did we get it?)

Integer Arithmetic
◆Integers in MIPS can be represented as unsigned
form or 2’s complement form – 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, d all are registers)
– meaning of “u”: overflow is ignored; note: it does not
mean “unsigned” ◆add d, 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
– 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 contrast 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 operands are small, mfhi is filled with zeros

Division Instructions: div and divu
◆With N-bit integer division, we have two results of N- bit each
– op1 / op2: op1 = op2 x quotient + remainder – again, we need hi and lo to store the results
◆div s, t and divu s, t
– divide s by t; div for 2’s complement, divu for unsigned

Summary of Today’s 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

Summary of Today’s 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

Summary of Today’s Lecture ◆Brief introduction to MIPS
– register-based architecture
– Reduced Instruction Set Computer (RISC)
– three key points: (1) registers (2)

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com