Package: mipsplusplus
MIPS++ Programming LanguageMIPS++is a low-level programming language based on theMIPS architecture. Its purpose is to generate faithful MIPS assembly code using a clearer syntax, with a focus on optimization. It is also a superset of MIPS.Current features include arithmetic expressions, data types, register aliases (i.e. local variables), conditional jumps and arrays.UsageInstalling:pip install mipsplusplusCommand line:python -m mipsplusplus path/to/source.mpp -o output.asm
--comments 1 --registers "$t0" "$t1" "$t1"Python:importmipspluspluscompiled=mipsplusplus.compile(lines=source.readlines(),comments=1,registers=['$t0','$t1','$t2'])Optional Parameterscomments:The level of commenting in the generated MIPS code.
0 = None, 1 = Minimal, 2 = Almost every line. Defaults to 1.registers:List of temporary registers (in order) for the compiler to use where necessary. These should not be used to store variables as they may be overridden. The compiler will indicate if more are required for any particular statement. Defaults to[$t0, $t1, $t2].Syntax ExampleThe following program converts a decimal number to an arbitrary base..databyte[30]digitsstringnumPrompt="Enter number: "stringbasePrompt="Enter base: "stringconvertedInfo="Converted: ".text@alias$num=$t3,$base=$t4,$i=$t5printnumPrompt$num=input()printbasePrompt$base=input()$i=0calcLoop:digits[$i]=$num%$base$num=$num/$base$i=$i+1gotocalcLoopif$num>0printconvertedInfoprintLoop:$i=$i-1gotoendPrintLoopifi<0@alias$digit=$t3$digit=digits[$i]gotoprintAsCharif$digit>9print$digitgotoprintLoopprintAsChar:print(55+$digit)aschargotoprintLoopendPrintLoop:exitLanguage ReferenceDefinitions.data# Numericintx=123456789# 4 bytesshorty=-32000# 2 bytesbytez=64# 1 bytecharletter='C'# 1 byte# Addresses/arraysstringgreeting="hello"char[20]name# Empty string with space for 20 charactersint[]numbers=[20,-65,42]byte[30]byteArray.text# ...codeRegisters and Variables# Any register can be given an alias, which remains# for all following lines or until it is reassigned@alias$num=$t3,$array=$t4,$character=$t5# Direct register assignment$num=42$character='K'# Loading from defined variables$num=x$array=numbers$character=letter# Saving to defined variablesx=$numletter='M'y=letter# Uses ASCII valueOperators# Standard$num=(x+y)/(z-7)$num=neg$num*(y%8)# Boolean logic$true=1$false=0$bool=not($trueand$false)nor$truexor($trueor$false)# Bitwise$num=(1<<4)and($num>>1)# Zeros are shifted in$num=-8>>>2# The sign bit is shifted in# Note that <<< is not an operator# Comparison$condition=z<200# Note that this is the only comparison operator# which can be used in arbitrary expressionsAddresses# Addresses$numAddress=addressofx# Requires explicit 'addressof' operator$stringAddress=greeting$arrayAddress=numbers$offsetAddress=$numbers+4$offsetAddress=addressof$numbers[4]# Equivalent to previous lineArrays# Byte arrays$num=byteArray[3]byteArray[4]=42+$num-z# Integer arrays# (indexes must be multiplied by the number# of bytes in the corresponding data type)num=numbers[$index*4]numbers[24]=32# Sets the 6th element# Ambiguous arrays$array=someArray# - Explicit$num=$array[0]asint# Load value as int$array[4]asint=$num# Store value as int# - Implied$array[8]=42# Store int$array[9]=42asbyte# Store byte$array[10]=$num+y+3# Store short$array[11]=$array[12]aschar# Load and store char (byte)# - Combined$array1[13]asint=6*($array2[$i*2]asshort)# Load short, store int$array3[14]=(22-numbers[$i*4])asbyte# Load int, store byteControl Flowlabel:# ...# Jumping to labelsgotolabelgotolinklabel# Return address is stored in $ra# Jumping to addresses in registers$storedLabel=label# (addressof label)goto$storedLabel# Conditional jumps# (supports all comparison operators)gotolabelifx>9gotolabelif$num+4!=xgoto$storedLabelif$num%7<=x/ygoto$storedLabelif$condition1andx<8# ($condition1 and x < 8) > 0# - With link (only < 0 and >= 0)gotolinklabelif$num<0gotolink$storedLabelif$num>=0System Functions# Printingprint$numprintgreetingprint$characteraschar# Input integer$num=input()x=input()+6input()# Value is stored in $v0# Input stringinputstrnameinputstraddressof$array[8]inputstr$location,40# Max length 40# Memory allocationaddress=alloc(50)# Alocate 50 bytes, store first address as int$array=alloc(20)+4# Offset by 4alloc(80)# Address is stored in $v0# Exitexit