GIDForums  

Go Back   GIDForums > Computer Programming Forums > Assembly Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 29-May-2009, 12:24
kizursoze kizursoze is offline
New Member
 
Join Date: May 2009
Posts: 2
kizursoze is on a distinguished road

PC SPIM disassembler


Hi guys!

I am supposed to finish a skeleton file which disassembles machine code into MIPS instructions. The problem statement follows.....

Quote:
In this assignment, you will implement a simple disassembler program using MIPS assembly language for the Mini-MIPS ISA defined below. You will use the MIPS functional simulator for validating the functionality of your MIPS disassembler program. program should disassemble only the six instructions defined in the mini-MIPS ISA from the binary words provided. All other words not belonging to any of the six instructions should print out “unsupported op¬code !!” First, download the lab1skel.s skeleton file which is attached to this assignment. This skeleton file contains 14 instructions defined after the label “binary_code” in hexadecimal format. Each instruction is encoded by each .word using Little Endian byte-order. Your MIPSprogram should read one .word at a time, decode the .word binary to determine the instruc¬tion, and then print out each instruction on the Console window. Note that some instruc-tions in the skeleton program are not defined by the Mini-MIPS ISA. During disassembly, your program should print out “Unsupported opcode !!“ on the console window when encoun¬tering such instructions . Be sure to note that the Offset field encodes a signed number, becautious when you print them out. For jump target, assume that the four msbs of the PC are 0000.

Hint. In the skeleton file, a few ASCII (.asciiz) symbols and instruction mnemonics (e.g. bne) are defined. You can use them to print out some of the needed instruction symbols in the Console window. There are many repetitive operations to carry out in your program (e.g.print dollar sign ($) before print the register number). In order to reduce the code size, youare encouraged to use procedure/function call, e.g. jal instruction for call and its corre¬sponding jr $31 for return in MIPS, to handle these repetitive operations. If you are careful in how you allocate your registers, you will not need to use the stack to store the proce¬dure‘s local variables etc. An example function might extract an unsigned 5-bit number froma register, given a bit offset into the register.


And the six commands are:

1.JPG

And the .s file:

Code:
## Disassembler Program ## ## ## .data .globl binary_code binary_code: .word 0x0c10000d .word 0x2010ffff .word 0x3c11ffff .word 0x22310000 .word 0x3c12ff00 .word 0x22527fff .word 0x02329824 .word 0x3c141001 .word 0x22940004 .word 0x8e95fffc .word 0x02b3b02a .word 0xae960000 .word 0x06600002 .word 0x03e00008 .word 0xae930004 .word 0x03e00008 # delimiter .word 0x00000000 .word 0x00000000 dollar: .asciiz "$" space: .asciiz " " caret: .asciiz "\n" lparen: .asciiz "(" rparen: .asciiz ")" Ilw: .asciiz "lw " # you have to define all the symbols yourself unsupport: .asciiz "Unsupported opcode !!\n" pend: .asciiz "Done!\n" .align 5 scratchmem: .space 8192 # Standard startup code. Invoke the routine main with no arguments. .text .globl __start __start: jal main addu $0, $0, $0 # Nop addiu $v0, $0, 10 syscall # syscall 10 (exit) .globl main main: addu $25, $0, $31 # Save return PC, if you try to use $25, back it up before you use it ### My code starts here ### ### My code ends here ### Exit: li $v0, 4 la $a0, pend syscall jr $25

And here is basically what the output console will look like:

2.JPG

This is the first course that I have had to use SPIM in, but I have been reading up on it. And have a little programming experience with C++ and Matlab. I understand the basic commands / structure of SPIM, but I don't know if there is any way to directly access the binary_code data. I haven't been able to find much information on accessing globl data online. I am using PC SPIM ver. 7.2.1 on Windows XP if it makes any difference, and any help will be greatly appreciated!
  #2  
Old 04-Jun-2009, 14:21
kizursoze kizursoze is offline
New Member
 
Join Date: May 2009
Posts: 2
kizursoze is on a distinguished road

Re: PC SPIM disassembler


Ok guys, for those wanting to see the final result here it is...

Code:
.data .globl binary_code binary_code: .word 0x0c10000d .word 0x2010ffff .word 0x3c11ffff .word 0x22310000 .word 0x3c12ff00 .word 0x22527fff .word 0x02329824 .word 0x3c141001 .word 0x22940004 .word 0x8e95fffc .word 0x02b3b02a .word 0xae960000 .word 0x06600002 .word 0x03e00008 .word 0xae930004 .word 0x03e00008 # delimiter .word 0x00000000 .word 0x00000000 dollar: .asciiz "$" space: .asciiz " " caret: .asciiz "\n" lparen: .asciiz "(" rparen: .asciiz ")" comma: .asciiz "," ### Command Labels ################################################### J: .asciiz " JAL " # jump and link adi: .asciiz " addi " # add immediate low: .asciiz "lw " # load word stw: .asciiz " sw " # store word bltze: .asciiz " bltz " # branch less than zero annd: .asciiz " and " # and ##################################################### us: .asciiz "Unsupported opcode !!\n" pend: .asciiz "Thank Goodness I'm Done!\n" .align 5 scratchmem: .space 8192 # Standard startup code. Invoke the routine main with no arguments. .text .globl __start __start: jal main addu $0, $0, $0 # Nop addiu $v0, $0, 10 syscall # syscall 10 (exit) .globl main main: addu $25, $0, $31 # Save return PC, if you try to use $25, back it up before you use it ### My code starts here ### li $s1 , 0x0c000000 # 3 jump and link li $s2 , 0x20000000 # 8 add immediate li $s3 , 0x8c000000 # 0x23 load word li $s4 , 0xac000000 # 2b store word li $s5 , 0x04000000 # 1 branch on less than zero # 0 and use $zero for comparison li $s7 , 0x00000000 # for delimiter check update: ## increments the pointer for $a1 containing binary_code la $a1 , binary_code # load binary_code into $s1 lw $t0 , 0($a1) jal nap lw $t0 , 4($a1) jal nap lw $t0 , 8($a1) jal nap lw $t0 , 12($a1) jal nap lw $t0 , 16($a1) jal nap lw $t0 , 20($a1) jal nap lw $t0 , 24($a1) jal nap lw $t0 , 28($a1) jal nap lw $t0 , 32($a1) jal nap lw $t0 , 36($a1) jal nap lw $t0 , 40($a1) jal nap lw $t0 , 44($a1) jal nap lw $t0 , 48($a1) jal nap lw $t0 , 52($a1) jal nap lw $t0 , 56($a1) jal nap lw $t0 , 60($a1) jal nap lw $t0 , 64($a1) jal nap lw $t0 , 68($a1) jal nap lw $t0 , 72($a1) jal nap nap: # loop begins and checks the first 6 bits of each op code to determine wher to branch to beq $t0 , $zero , Z # Allow for exit on 2 all zeros lui $t1 , 0xfc00 # Load 6 msb's of $t1 with 1's and $t1 , $t0 , $t1 # in order to check the 6 msbs of $to ############################################################################## beq $t1 , $s1 , A # Test for the 7 possible values beq $t1 , $s2 , B beq $t1 , $s3 , C beq $t1 , $s4 , D beq $t1 , $s5 , E beq $t1 , $zero , F # Unsupported op code label li $v0, 4 la $a0, us syscall li $v0, 4 la $a0, caret syscall jr $ra ############################################################################## A: # jump and link output labeling code li $v0, 4 la $a0, J syscall li $v0, 4 la $a0, space syscall li $t3 , 0x03ffffff and $t3 , $t0 , $t3 li $v0 , 1 la $a0 , 0($t3) syscall li $v0, 4 la $a0, caret syscall li $v0, 4 la $a0, caret syscall jr $ra B: # add immediate labeling code li $v0, 4 la $a0, adi syscall li $v0, 4 la $a0, space syscall li $v0, 4 la $a0, dollar syscall li $t4 , 0x001f0000 and $t4 , $t0 , $t4 srl $t4 , $t4 , 16 li $v0 , 1 la $a0 , 0($t4) syscall li $v0, 4 la $a0, comma syscall li $v0, 4 la $a0, space syscall li $v0, 4 la $a0, dollar syscall li $t3 , 0x03e00000 and $t3 , $t0 , $t3 srl $t3 , $t3 , 21 li $v0 , 1 la $a0 , 0($t3) syscall li $v0, 4 la $a0, comma syscall li $v0, 4 la $a0, space syscall li $t5 , 0x0000ffff and $t5 , $t0 , $t5 li $v0 , 1 la $a0 , 0($t5) syscall li $v0, 4 la $a0, caret syscall li $v0, 4 la $a0, caret syscall jr $ra C: # load word labeling code li $v0, 4 la $a0, low syscall li $v0, 4 la $a0, space syscall li $v0, 4 la $a0, dollar syscall li $t4 , 0x001f0000 and $t4 , $t0 , $t4 srl $t4 , $t4 , 16 li $v0 , 1 la $a0 , 0($t4) syscall li $v0, 4 la $a0, comma syscall li $v0, 4 la $a0, space syscall li $t5 , 0x0000ffff and $t5 , $t0 , $t5 li $v0 , 1 la $a0 , 0($t5) syscall li $v0, 4 la $a0, lparen syscall li $v0, 4 la $a0, dollar syscall li $t3 , 0x03e00000 and $t3 , $t0 , $t3 srl $t3 , $t3 , 21 li $v0 , 1 la $a0 , 0($t3) syscall li $v0, 4 la $a0, rparen syscall li $v0, 4 la $a0, caret syscall li $v0, 4 la $a0, caret syscall jr $ra D: # store word labeling code li $v0, 4 la $a0, stw syscall li $v0, 4 la $a0, space syscall li $v0, 4 la $a0, dollar syscall li $t4 , 0x001f0000 and $t4 , $t0 , $t4 srl $t4 , $t4 , 16 li $v0 , 1 la $a0 , 0($t4) syscall li $v0, 4 la $a0, comma syscall li $v0, 4 la $a0, space syscall li $t5 , 0x0000ffff and $t5 , $t0 , $t5 li $v0 , 1 la $a0 , 0($t5) syscall li $v0, 4 la $a0, lparen syscall li $v0, 4 la $a0, dollar syscall li $t3 , 0x03e00000 and $t3 , $t0 , $t3 srl $t3 , $t3 , 21 li $v0 , 1 la $a0 , 0($t3) syscall li $v0, 4 la $a0, rparen syscall li $v0, 4 la $a0, caret syscall li $v0, 4 la $a0, caret syscall jr $ra E: # branch on less than zero labeling code li $v0, 4 la $a0, bltze syscall li $v0, 4 la $a0, space syscall li $v0, 4 la $a0, dollar syscall li $t3 , 0x03e00000 and $t3 , $t0 , $t3 srl $t3 , $t3 , 21 li $v0 , 1 la $a0 , 0($t3) syscall li $v0, 4 la $a0, comma syscall li $v0, 4 la $a0, space syscall li $t3 , 0x0000ffff and $t3 , $t0 , $t3 li $v0 , 1 la $a0 , 0($t3) syscall li $v0, 4 la $a0, caret syscall li $v0, 4 la $a0, caret syscall jr $ra F: # and labeling code li $t1 , 0x00000024 # to check the last 6 bits li $t9 , 0x00000024 and $t1 , $t1 , $t0 bne $t1 , $t9 , unsupport2 li $v0, 4 la $a0, annd syscall li $v0, 4 la $a0, space syscall li $v0, 4 la $a0, dollar syscall li $t6 , 0x0000f800 # $rd and $t6 , $t0 , $t6 srl $t6 , $t6 , 11 li $v0 , 1 la $a0 , 0($t6) syscall li $v0, 4 la $a0, comma syscall li $v0, 4 la $a0, space syscall li $v0, 4 la $a0, dollar syscall li $t3 , 0x03e00000 # $rs and $t3 , $t0 , $t3 srl $t3 , $t3 , 21 li $v0 , 1 la $a0 , 0($t3) syscall li $v0, 4 la $a0, comma syscall li $v0, 4 la $a0, space syscall li $v0, 4 la $a0, dollar syscall li $t4 , 0x001f0000 # $rt and $t4 , $t0 , $t4 srl $t4 , $t4 , 16 li $v0 , 1 la $a0 , 0($t4) syscall li $v0, 4 la $a0, caret syscall li $v0, 4 la $a0, caret syscall jr $ra unsupport2: # in case the last 6 bits of the and block didn't match up li $v0, 4 la $a0, us syscall li $v0, 4 la $a0, caret syscall jr $ra Z: bne $s7 , $zero , Exit # if zero counter = zero increment by 4 and loop , else exit addi $s7 , 4 jr $ra ### My code ends here ### Exit: li $v0, 4 la $a0, pend syscall jr $25

It ain't pretty to look at, but at least I am done! It works pretty well for the most part. Although, there were some sort of errors given by SPIM.
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:21.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.