Download Machine Language Class Exercise Answer Answer

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Machine Language
01000100
01010101
11000100
10000100
; read data in memory address 0100 to register 00
; read data in memory address 0101 to register 01
; add value in register 01 to register 00
; write data in register 00 to memory address 0100
If you like, the logic of this machine language program
can be represented in a high level language such as
C:
x = x + y;
Class Exercise
If there are four values 20, 30, 50, 70
stored in the memory that the memory
addresses are 0000, 0001, 0010, 0011.
Can you help me writing a machine
program for adding them up?
Answer
• Read from memory to the register
• Do the calculation
• Write from register to memory
Answer
01000000
01010001
11000100
10000100
01000100
01010010
11000100
10000100
01000100
01010011
11000100
10000100
; read data in memory address 0000 to register 00
; read data in memory address 0001 to register 01
; add value in register 01 to register 00
; write data in register 00 to memory address 0100
; read data in memory address 0100 to register 00
; read data in memory address 0010 to register 01
; add value in register 01 to register 00
; write data in register 00 to memory address 0100
; read data in memory address 0100 to register 00
; read data in memory address 0011 to register 01
; add value in register 01 to register 00
; write data in register 00 to memory address 0100
1
Problems
Execution Steps
1. Load the program to the Main Memory
• 12 instructions, use 5 registers
• 12 instructions, Too long?
2. CPU get each instruction from Main
Memory
Use all registers?
Are they feasible?
3. CPU execute it one by one
=> how can we run this instruction?
Qs on Execution Steps
How can we know which address of the
next instruction to be executed?
a specialized register called
Program Counter (PC)
How can we store the an instruction has
been read from memory?
a specialized register called
Instruction Register (IR)
Review the question
• Use all registers?
Some have special uses.
• 12 instructions, Too long?
Calculate the memory size
12 memory address + 5 memory
address = 17 memory address!!!
2
Assembly language
Assembly language
• machine language - difficult to read,
write or understand
• better way => assembly language
(be a human-readable)
• Assign (mnemonic) names to each
opcode and each register.
Register
2-bit binary label
Mnemonic Name
Register 1
00
AX
Register 2
01
BX
Register 3
10
PC
Register 4
11
IR
Mnemonic opcode
Description
RST
00
Reset a register to 0
RD
01
Read a byte from memory and store it into a register
WR
10
Write a byte from a register to a memory location
ADD
11
Add the value in a register to another register
Assembly language
With mnemonics, we can now rewrite our
sample machine language program so that
it is much easier to read and understand.
To ease your comparison, both the
machine language version and the
mnemonic version are shown below
Assembly language
01000100
01010101
11000100
10000100
RD AX 0100
RD BX 0101
ADD AX BX
WR AX 0100
; read data in memory address 0100 to register 00
; read data in memory address 0101 to register 01
; add value in register 01 to register 00
; write data in register 00 to memory address 0100
; read data in memory address 0100 to register AX
; read data in memory address 0101 to register BX
; add value in BX to AX
; write data in AX to memory address 0100
3
Class Exercise
Answer
RD AX 0000 ; read data in memory address 0000 to register 00
RD BX 0001 ; read data in memory address 0001 to register 01
If there are four values 20, 30, 50, 70
stored in the memory that the memory
addresses are 0000, 0001, 0010, 0011.
Can you help me writing an assembly
program for adding them up?
ADD AX BX ; add value in register 01 to register 00
WR AX 0100 ; write data in register 00 to memory address 0100
RD AX 0100 ; read data in memory address 0100 to register 00
RD BX 0010 ; read data in memory address 0010 to register 01
ADD AX BX ; add value in register 01 to register 00
WR AX 0100 ; write data in register 00 to memory address 0100
RD AX 0100 ; read data in memory address 0100 to register 00
RD BX 0011 ; read data in memory address 0011 to register 01
ADD AX BX ; add value in register 01 to register 00
WR AX 0100 ; write data in register 00 to memory address 0100
MIPS Overview
Writing the Assembly Language
in the MIPS platform
4
MIPS CPU
MIPS registers
> 32 general-purpose
¾32-bit registers
¾Some have special uses, like k0, k1, are
reserved for OS kernel.
Table 1: MIPS registers and the convention governing their use
Memory Space
Memory Space
• the user address space (0x400000) is
the text segment, which holds the
instructions for a program
• Above the text segment is the data
segment (starting at 0x10000000)
Figure 2: Layout of Memory
5
Class Exercise
Mnemonic
Format
Description
li
li Rdest, imm
Move the immediate imm into register
Rdest.
add
add Rdest, Rsrc1, Src2
Put the sum of the integers from register
Rsrc1 and Src2 (or Imm) into register
Rdest.
Can you help me to write down a small assembly
program to add 10 and 6?
The result can leave it in the register.
Compilation
Answer
li $t1, 6
li $t2, 10
add $t0, $t1, $t2
Assembler
• translates assembly language into
binary instructions
• source file -> object file
Figure 3: The process that produces an executable file.
• Linker: object and library files -> an
executable file
6