#NASM-MIPS Translations
This is a WIP. Please notify me of any mistakes or possible improvements.
I have ignored any 64-bit differences due to Moore seeming to be a 32-bit system.
#Contents
Registers
Basic Instructions
Data Instructions
Bitwise Instructions
Logic Instructions
Appendix
#Registers
Purpose of Register | MIPS | NASM |
---|---|---|
Stack Pointer | $sp | esp |
Stack Frame Pointer / Base Pointer | $fp | ebp |
Return Value | $v0-$v1 | eax |
Return Value (floating point) | $f0 | st0 |
Arguments | $a0-$a3 | NONE - In stack |
Free (32-bit integer) | $t0-$t9, $s0-$s8 | eax, ecx, ebx, edx, esi, edi(ebx if not in a shared library) |
Free (16-bit integer) | NONE | ax, bx, cd, dx |
Free (8-bit integer) | NONE | ah, al, bh, bl, ch, cl, dh, dl |
Free (floating point) | $f0-$f31 | st0,st1,...,st? |
Return Address | $ra | NONE - In stack |
Product and Remainder | $LO, $HI | eax, edx |
Quotient and High 32 Bits | $LO, $HI | eax, edx |
#Basic Instructions
Arbitrarily chosen names are shown in italics.
There are several more pseduoinstructions that are combinations of the ones shown here.
Description | MIPS | NASM |
---|---|---|
Load immediate value into register. | li $t0, 4 | mov eax,4 |
Move value of one register into another. | move $t0, $t1 | mov eax,ecx |
Add 2 registers. | add $t0, $t1, $t2 | add eax,ebx (Sum saved in first register) |
Add register and immediate. | addi $t0, $t1, 4 | add eax,4 (Sum saved in first register) |
Subtract 2 registers. | sub $t0, $t1, $t2 | sub eax,ebx (Difference saved in first register) |
Subtract register and immediate. | subi $t0, $t1, 4 | sub eax,4 (Difference saved in first register) |
Multiply. | mult $t1, $t2 (Result in $HI and $LO) | mul ebx (Multiply eax by ebx and put product into eax.) |
Multiply by an immediate. | multi $t1, 3 (Result in $HI and $LO) | imul ebx, 3 (Multiply ebx by 3 and put product into ebx.) |
Divide. | div $t1, $t2 (Result in $HI and $LO) | div ebx (Divide eax by ebx and put quotient into eax.) |
Divide by an immediate. | divi $t1, 3 (Result in $HI and $LO) | idiv ebx, 3 (Divide ebx by 3 and put quotient into ebx.) |
Add 1 to a register. | addi $t1, $t1, 1 | inc eax |
Subtract 1 from a register. | subi $t1, $t1, 1 | dec eax |
Call a function. | jalr address of function | call name of function |
Import an external function. | .global name of function | extern name of function |
Return | jr $ra | ret (Returns to the last jump instruction called.) |
#Data Instructions
Description | MIPS | NASM |
---|---|---|
Push a value into the stack. | subi $sp, $sp, 4; sw $t0, ($sp) | push eax |
Pop a value from the stack. | lw $t0, ($sp); addi $sp, $sp, 4 | pop eax |
Declare initialized variable. (Word) | variableName: .word 3 | variableName: dw 3 |
Declare uninitialized variable. (Word) | NONE - Must initialize. | variableName: resd 1 (1 is # of words) |
Declare initialized variable. (Byte) | variableName: .byte 3 | variableName: db 3 |
Declare uninitialized variable. (Byte) | NONE - Must initialize. | variableName: resb 1 (1 is # of bytes) |
Declare initialized variable. (Array) | NONE - Initialize after. | variableName: db 3 |
Declare uninitialized variable. (Array) | variableName: .space 10 (Array of 10 words) | variableName: resq 10 (Array of 10 reals) |
#Bitwise Instructions
Description | MIPS | NASM |
---|---|---|
Shift logical left << | sll $t0, $t0, 4 | shl eax, 4 |
Shift logical right >> | srl $t0, $t0, 4 | shr eax, 4 |
Bitwise NOT | not $t0, $t0 | not eax |
Bitwise AND | not $t0, $t0, $t1 | not eax, ebx |
Bitwise OR | xor $t0, $t0, $t1 | xor eax, ebx |
Bitwise XOR | or $t0, $t0, $t1 | or eax, ebx |
#Logic Instructions ##MIPS
Instruction | Description |
---|---|
b label | Unconditional branch. |
beq $t0, $t1, label | Branch if $t0 = $t1 |
bgez $t0, label | Branch if $t0 >= 0 |
bgezal $t0, label | Branch if $t0 >= 0 and set $ra |
bgtz $t0, label | Branch if $t0 > 0 |
bgtz $t0, label | Branch if $t0 < 0 |
bgtzal $t0, label | Branch if $t0 < 0 and set $ra |
bne $t0, $t1, label | Branch if $t0 != $t1 |
j address | Jump to address. |
jr $t0 | Jump to address stored in register. |
jal address | Jump to address and set $ra. |
jalr $t0 | Jump to address stored in register and set $ra. |
labelname: | Define a label. |
##NASM
Instruction | Description |
---|---|
cmp eax, ebx | Set comparison flags for two values. |
jl label | Jump to label if eax < ebx |
jle label | Jump to label if eax <= ebx |
je label | Jump to label if eax = ebx |
jge label | Jump to label if eax >= ebx |
jg label | Jump to label if eax > ebx |
jne label | Jump to label if eax != ebx |
labelname: | Define a label. |
#Appendix ##A - Getting the program counter in MIPS.
bgezal $zero, getpc
getpc:
move $v0, $ra
j $ra
##B - NASM setup instructions.
Instruction | Definition |
---|---|
pusha | Push all registers. |
popa | Pop all registers. |
enter | Create the stack frame. (Gives you a place to push) |
leave | Destroy the stack frame. (Frees up the memory taken by enter) |
##C - NASM Calling Procedure ###Caller
push ARGUMENT#N ;Push arguments
;...
push ARGUMENT#3
push ARGUMENT#2
push ARGUMENT#1
call FUNCTION ;Call function
add esp, 4*N ;Essentially pops arguments.
##Callee
enter 0,0 ;Create stack frame
mov ARGUMENT#1, dword [ebp+8] ;Load arguments.
mov ARGUMENT#2, dword [ebp+12]
mov ARGUMENT#3, dword [ebp+16]
;...
mov ARGUMENT#N, dword [ebp+4+N*4]
;YOUR FUNCTION CODE HERE!
leave ;Destroy stack frame
ret ;Return to caller
#Sources
http://www.nasm.us/doc/nasmdoc3.html
https://www.csee.umbc.edu/courses/undergraduate/CMSC313/fall04/burt_katz/lectures/
http://www.eecg.toronto.edu/~amza/www.mindsec.com/files/x86regs.html
https://msdn.microsoft.com/en-us/library/ms253512(VS.80).aspx
http://www.mrc.uidaho.edu/mrc/people/jff/digital/MIPSir.html
https://www.cs.uaf.edu/2006/fall/cs301/support/x86/
http://home.myfairpoint.net/fbkotler/nasmdocc.html
https://en.wikibooks.org/wiki/X86_Assembly/Other_Instructions
http://www.nasm.us/doc/nasmdoc9.html
https://en.wikipedia.org/wiki/MIPS_instruction_set#Pseudo_instructions