MIPS Assembly Language (P2)

Security for Newbie VSEC - BLOG

Gone is the one… the autumn coat is folded

Still hiding a bit of golden sunlight in the flap

My sorrow growing on branches as leaves

Not fall off quickly but wait for another autumn

Hello everyone, it’s me again, an amateur entering the field. Lately, due to being swamped with company projects, I haven’t had the time to explore and write blog anymore . But today, on the occasion of a leisurely day, just like my single status during the winter season – I take some time to continue this ongoing series, hoping you’ll continue reading?).

Arithmetic Computational Instructions.

Addition Instruction.

As far as I know, it includes:

Instruction Example Meaning
add add $1,$2,$3 $1=$2+$3
add immediate addi $1,$2,100 $1=$2+100 It means addition with constant numbers
add unsigned addu $1,$2,$3 $1=$2+$3 Unsigned integer addition,
add immediate unsigned addiu $1,$2,100 $1=$2+100 Unsigned integer addition,

 

Syntax:

For add, addu: <instruction name> <destination register>, <source register 1>, <source register 2>

For addi, addiu: <instruction name> <destination register>, <source register 1>, <constant number>

Specific example:

Compiling the above .c file with the MIPS architecture as follows:

mips-linux-gnu-gcc -O3 -S -mfp32 -march=mips32 Sum.c

Image when compiled to MIPS Assembly.

Why do I declare a signed int, but the assembly output uses addiu? From what I’ve briefly researched online, most compilers tend to use addu and addiu to avoid “overflow” traps. The add and addi instructions seem to be used only when manually coding or in ISAs like x86, ARM…

Subtraction Instruction.

Instruction Example Meaning
subtract sub $1,$2,$3 $1=$2-$3
subtract unsigned subu $1,$2,$3 $1=$2-$3 Unsigned integer subtraction

Syntax:

sub,subu: <instruction name > <destination register> , <register 1>, < register 2>

Specific example: 

Image when compiled to MIPS Assembly.

Multiplication Instruction.

Instruction Example Meaning
Multiply (withoutoverflow) mul $1,$2,$3 $1=$2*$3 Result fits in 32 bits.
Multiply mult $2,$3 $hi,$low=$2*$3 High 32-bit overflow multiplication saved in hi register

Low 32 bits stored in low register.

Specific example:

Division Instruction

Lệnh Ví dụ Ý nghĩa
Divide div $2,$3 $hi,$low=$2/$3 Kết quả được lưu trong thanh ghi hi

Thươn số được lưu trong thanh ghi low

 

Alright, I’m too lazy to capture images for this part 😄. This is similar to the previous images anyway.

Logic Computational Instructions.

Instruction Example Meaning
and and $1,$2,$3 $1=$2&$3 Bitwise AND
or or $1,$2,$3 $1=$2|$3 Bitwise OR
xor xor $1,$2,$3 $1 = $2 ??$3 Bitwise XOR
nor nor $1,$2,$3 $1 = ~($2 | $3) not of OR Bitwise NOR
and immediate andi $1,$2,100 $1=$2&100 Bitwise AND register, constant number
or immediate or $1,$2,100 $1=$2|100 Bitwise OR register, constant number
xor immediate xori $1, $2,10 $1 = ~$2 &~10 Bitwise XOR register, constant number
shift left logical sll $1,$2,10 $1=$2<<10 Left shift
shift right logical srl $1,$2,10 $1=$2>>10 Right shift

Specific example.

Image when compiled to MIPS Assembly.

Ending Part 2 for the chilly winter here. See you in the next parts ^^.

Vu Van Tien ( aka n0_be3r )