MIPS3 - Assembly vs. machine language So far we've been...

55Assembly vs. machine language±So far we’ve been using assembly language.–We assign names to operations (e.g., add) and operands (e.g., $t0).–Branches and jumps use labels instead of actual addresses.–Assemblers support many pseudo-instructions.±Programs must eventually be translated into machine language, a binary format that can be stored in memory and decoded by the CPU.±MIPS machine language is designed to be easy to decode.–Each MIPS instruction is the same length, 32 bits.–There are only three different instruction formats, which are very similar to each other.±Studying MIPS machine language will also reveal some restrictions in the instruction set architecture, and how they can be overcome.

This preview
has intentionally blurred sections.
Sign up to view the full version.

58±Larger constants can be loaded into a register 16 bits at a time.–The load upper immediate instruction luiloads the highest 16 bits of a register with a constant, and clears the lowest 16 bits to 0s.–An immediate logical OR, ori, then sets the lower 16 bits.±To load the 32-bit value 0000 0000 0011 1101 0000 1001 0000 0000:lui $s0, 0x003D# $s0 = 003D 0000 (in hex)ori $s0, $s0, 0x0900# $s0 = 003D 0900±This illustrates the principle of making the common case fast.–Most of the time, 16-bit constants are enough.–It’s still possible to load 32-bit constants, but at the cost of two instructions and one temporary register.±Pseudo-instructions may contain large constants. Assemblers will translate such instructions correctly.±We used a lwinstruction before.Larger constants

59±The limited 16-bit constant can present difficulties for accesses to global data.–Let’s assume the assembler puts a variable at address 0x10010004.–0x10010004 is bigger than 32,767 ±In these situations, the assembler breaks the immediate into twopieces.lui$t0, 0x1001# 0x1001 0000lw$t1, 0x0004($t0)# Read from Mem[0x1001 0004]Loads and stores

This preview
has intentionally blurred sections.
Sign up to view the full version.

60±For branch instructions, the constant field is not an address, but an offsetfrom the nextprogram counter (PC+4) to the target address.beq$at, $0, Ladd$v1, $v0, $0add$v1, $v1, $v1jSomewhereL:add$v1, $v0, $v0±Since the branch target Lis three instructionspast the first add, the address field would contain 3×4=12. The whole beqinstruction would be stored as:000100 00001000000000 0000 0000 1100oprsrtaddressBranchesWhy (PC+4)? Will be clear when we learned pipelining

61±Empirical studies of real programs show that most branches go totargets less than 32,767 instructions away—branches are mostly used in loops and conditionals, and programmers are taught to make code bodies short.

This preview
has intentionally blurred sections.
Sign up to view the full version.