Advertisement
fastman92

[Python] MIPS li $at, float comment inserter

Apr 6th, 2014
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.59 KB | None | 0 0
  1. '''
  2.     Author: fastman92
  3.    
  4.     Description:
  5.         in MIPS executable you may often see instructions like this:
  6.             li      $at, 0x40555555
  7.            
  8.             0x40555555 is really a float 32-bit value.
  9.            
  10.             However since IDA Pro sees a non-float register ($at) it will not show a float, but an integer.
  11.            
  12.             This script will put a comment like this:
  13.                 # float: 3.33333           
  14. '''
  15.  
  16. import struct
  17.  
  18. APPLICATION_NAME = "MIPS li $at, float comment inserter"
  19.  
  20. def f2b(f):
  21.     return struct.unpack('I',struct.pack('f',f))[0]
  22.  
  23. def b2f(b):
  24.     return struct.unpack('f',struct.pack('I',b))[0]
  25.    
  26. def isWhole(x):
  27.     if(x%1 == 0):
  28.         return True
  29.     else:
  30.         return False   
  31.    
  32. def floatToStr(num):
  33.     if(isWhole(num)):
  34.         return "%g.0" % (num)
  35.     else:
  36.         return "%g" % (num)
  37.        
  38. def main():
  39.     Message("---- Start of MIPS li $at, float comment inserter 1.1 by fastman92 ----\n")               
  40.    
  41.     seg = FirstSeg()
  42.    
  43.     while seg != BADADDR:
  44.         loc = SegStart(seg)
  45.        
  46.         while loc != BADADDR and loc < SegEnd(seg):
  47.             if GetMnem(loc) == 'li':
  48.                 firstOp = GetOpnd(loc, 0)
  49.                 secondType = GetOpType(loc, 1)
  50.                
  51.                 if firstOp == "$at" and secondType == o_imm:
  52.                     comment = GetCommentEx(loc, False)         
  53.                    
  54.                     if comment == None or comment.startswith( 'float:'):
  55.                         value = GetOperandValue(loc, 1)
  56.                         value = b2f(value)
  57.                        
  58.                         MakeComm(loc, "float: " + floatToStr(value))
  59.                        
  60.                         print "0x%X - comment with float value %g inserted" % (loc, value)
  61.                        
  62.             loc = NextHead(loc, BADADDR)
  63.    
  64.         seg = NextSeg(seg)
  65.  
  66.     Message("---- End of MIPS li $at, float comment inserter 1.1 by fastman92 ----\n")   
  67.      
  68. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement