decoding machine language 反組譯

11
Decoding Machine Language 反反反 Jen-Chang Liu, Spring 2006 Adapted from http://www-inst.eecs.berkeley.edu/~cs61c/

Upload: jermaine-conrad

Post on 31-Dec-2015

24 views

Category:

Documents


5 download

DESCRIPTION

Decoding Machine Language 反組譯. Jen-Chang Liu, Spring 200 6 Adapted from http://www-inst.eecs.berkeley.edu/~cs61c/. 6. 5. 5. 5. 5. 6. R. opcode. target address. I. J. opcode. opcode. 6. rs. rs. 5. rt. rt. 5. rd. immediate. shamt. 16. funct. 26. 6. Review. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Decoding Machine Language  反組譯

Decoding Machine Language 反組譯

Jen-Chang Liu, Spring 2006

Adapted from

http://www-inst.eecs.berkeley.edu/~cs61c/

Page 2: Decoding Machine Language  反組譯

Review°Machine Language Instruction: 32 bits representing a single MIPS instruction

opcode rs rt rd functshamtopcode rs rt immediate

R

Iopcode target addressJ

6 5 5 5 65

6 5 5 16

6 26

Page 3: Decoding Machine Language  反組譯

Decoding Machine Language

°How do we convert 1s and 0s => assembly code => C code?

°For each 32 bits:• Look at opcode: 0 means R-Format, 2 or 3 mean J-Format, otherwise I-Format.

• Use instruction type to determine which fields exist and convert each field into the decimal equivalent.

• Once we have decimal values, write out MIPS assembly code.

• Logically convert this MIPS code into valid C code.

Page 4: Decoding Machine Language  反組譯

100123456789

101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263

100123456789

101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263

100123456789

101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263

0123456789

10111213141516171819202122232425262728293031

0123456789

10111213141516171819202122232425262728293031

0123456789

10111213141516171819202122232425262728293031

16000102030405060708090a0b0c0d0e0 f101112131415161718191a1b1c1d1e1 f202122232425262728292a2b2c2d2e2 f303132333435363738393a3b3c3d3e3 f

op(31:26)

jjalbeqbneblezbgtzaddiaddiusltisltiuandiorixoriluiz = 0z = 1z = 2z = 3

lblhlwllwlbulhulwr

sbshswlsw

swr

lwc0lwc1lwc2lwc3

swc0swc1swc2swc3

rs(25:21)mfcz

cfcz

mtcz

ctcz

copzcopz

(16:16)bcztbczt

tlbrtlbwi

tlbwrtlbp

rte

rt (20:16)

bltzbgez

bltzalbgezal

cvt.s.fcvt.d.f

cvt.w.f

c.f.fc.un.fc.eq.fc.ueq.fc.olt.fc.ult.fc.ole.fc.ule.fc.st.fc.ngle.fc.seq.fc.ngl.fc.lt.fc.nge.fc.le.fc.ngt.f

funct(5:0)add.fsub.fmul.fdiv.f

abs.fmov.fneg.f

funct(5:0)sll

srlsra

srlvsravjrjalr

syscallbreak

mfhimthimflomtlo

multmultudivdivu

addaddusubsubuandorxornor

sltsltu

if z = l,f = d

if z = l,f = s

if z = 0

01

funct(4:0)

Page 5: Decoding Machine Language  反組譯

Decoding Example (1/7)°Here are six machine language instructions in hex:

000010250005402A110000030044102020A5FFFF 08100001

°Let the first instruction be at address 4,194,30410 (0x00400000).

°Next step: convert to binary

Page 6: Decoding Machine Language  反組譯

Decoding Example (2/7)°Here are the six machine language instructions in binary:

0000000000000000000100000010010100000000000001010100000000101010000100010000000000000000000000110000000001000100000100000010000000100000101001011111111111111111 00001000000100000000000000000001

°Next step: identify opcode and format

1, 4-31 rs rt immediate0 rs rt rd functshamtR

IJ target address2 or 3

Page 7: Decoding Machine Language  反組譯

Decoding Example (3/7)°Select the opcode (first 6 bits)

to determine the format:

0000000000000000000100000010010100000000000001010100000000101010000100010000000000000000000000110000000001000100000100000010000000100000101001011111111111111111 00001000000100000000000000000001

°Look at opcode: 0 means R-Format,2 or 3 mean J-Format, otherwise I-Format.

°  Next step: separation of fields

RRIRIJ

Format:

Page 8: Decoding Machine Language  反組譯

Decoding Example (4/7)°Decimal:

0 0 0 2 3700 0 5 8 4204 8 0 +30 2 4 2 3208 5 5 -12 1,048,577

°Next step: translate to MIPS instructions

Page 9: Decoding Machine Language  反組譯

Decoding Example (5/7)°MIPS Assembly (Part 1):

0x00400000 or $2,$0,$00x00400004 slt $8,$0,$50x00400008 beq $8,$0,30x0040000c add $2,$2,$40x00400010 addi $5,$5,-10x00400014 j 0x100001

°Next step: translate to more meaningful instructions (fix the branch/jump and add labels)

Page 10: Decoding Machine Language  反組譯

Decoding Example (6/7)°MIPS Assembly (Part 2):

or $v0,$0,$0Loop: slt

$t0,$0,$a1beq $t0,$0,Fin

add $v0,$v0,$a0addi $a1,$a1,-1

j LoopFin:

°Next step: translate to C code (be creative!)

Page 11: Decoding Machine Language  反組譯

Decoding Example (7/7)°C code:

• Mapping: $v0: product$a0: mcand

$a1: mplier

product = 0;while (mplier > 0) {

product += mcand; mplier -= 1; }