Presentation is loading. Please wait.

Presentation is loading. Please wait.

Assembly language: arithmetic and load/store instructions Ellen Spertus MCS 111 September 17, 2002.

Similar presentations


Presentation on theme: "Assembly language: arithmetic and load/store instructions Ellen Spertus MCS 111 September 17, 2002."— Presentation transcript:

1 Assembly language: arithmetic and load/store instructions Ellen Spertus MCS 111 September 17, 2002

2 2 Big picture

3 3 The role of assembly language Every instruction in a high-level programming language (such as Java) is converted into several assembly instructions. A program called an assembler converts each assembly instruction to a machine instruction. The hardware “knows how to” run machine instructions.

4 4 Review: hardware Flip-flops (aka registers) –Can remember its old input –Can remember a new input Multiplexers –Choose one of many inputs Selector –Select one of many outputs

5 5 Instruction execution

6 6 Addition add $s1, $s2, $s3 –Meaning: $s1 = $s2 + $s3 –Operation: add –Operands: $s1, $s2, $s3 –Exactly three operands –Each operand must be a register

7 7 Translating Java code int a; int b; int c; a = b + c; a: $s1 b: $s2 c: $s3

8 8 A closer look at registers The MIPS architecture has 32 registers Each register is bits wide Registers corresponding to Java variables are called $s0, $s1,... Registers corresponding to temporary (unnamed) values are called $t0, $t1,...

9 9 Converting complex instructions a = b + c + d; add $t0, $s2, $s3 add $s1, $t0, $s4 a: $s1 b: $s2 c: $s3 d: $s4

10 10 Practice b = b + c + a; a: $s1 b: $s2 c: $s3

11 11 Subtraction sub $s1, $s2, $s3 –Meaning: $s1 = $s2 - $s3 –Operation: sub –Operands: $s1, $s2, $s3 –Exactly three operands –Each operand must be a register

12 12 Practice a = a - (b + d); a: $s1 b: $s2 c: $s3 d: $s4

13 13 Practice a = b - c - (a + d); a: $s1 b: $s2 c: $s3 d: $s4

14 14 Translation steps Java code: a = b + c Assembly code: add $s1, $s2, $s3 Machine code: 00000010010100111000100000100000

15 15 The need for more memory Why aren’t 32 registers enough memory for a modern computer?

16 16

17 17 Main memory How big?

18 18 Load/store instructions Purpose: Transfer data between registers and memory Load instructions –lb: load byte –lw: load word Store instructions –sb: store byte –sw: store word

19 19 Load-byte instruction Transfer data from memory to a register Load-byte: lb –Format:lb $ra, i($rb) –Example: lb $t2, 2($t0) –Meaning: Take the value in memory location (2+$t0) and put it in $t2 Note: i stands for immediate

20 20 Example: lb and sb count sum sum = sum + count; Assume $t0 = 0 lb $t2, 2($t0) lb $t1, 1($t0) add $t1, $t1, $t2 sb $t1, 1($t0) 8 bits

21 21 Bytes and words } one byte one word (4 bytes) 8 bits

22 22 How to combine bytes into word Little-endian –The “little end” (least significant byte) is stored first in memory. –Used by Intel Big-endian –The “big end” (most significant byte) is stored first in memory. –Used by MIPS, Motorola

23 23 Bytes and words } one byte one word (4 bytes) 8 bits

24 24 Word example count sum sum = sum + count; Assume $t0 = 0 lw $t2, _($t0) lw $t1, _($t0) add $t1, $t1, $t2 sw $t1, _($t0)

25 25 Summary: levels of languages People like to program in high-level languages, such as Java. The hardware knows how to run low- level machine code. High-level code is converted into machine language by compilers, assemblers, and students.

26 26 Summary: assembly language The add and sub operations act on registers. The load and store operations transfer data between memory and registers. How to order bytes within a word? –Little-endian –Big-endian –Bi-endian or “byte-sexual”

27 27

28 28 “His Majesty desired I would take some other opportunity of bringing all the rest of his enemy's ships into his ports. And so unmeasurable is the ambition of princes, that he seemed to think of nothing less than reducing the whole empire of Blefuscu into a province, and governing it by a Viceroy; of destroying the Big-Endian exiles, and compelling that people to break the smaller end of their eggs, by which he would remain the sole monarch of the whole world.” — Gulliver’s Travels by Jonathan Swift


Download ppt "Assembly language: arithmetic and load/store instructions Ellen Spertus MCS 111 September 17, 2002."

Similar presentations


Ads by Google