Presentation is loading. Please wait.

Presentation is loading. Please wait.

How to represent signed integers

Similar presentations


Presentation on theme: "How to represent signed integers"— Presentation transcript:

1 How to represent signed integers
4. Biased (excess) B = 3 p x 000 -3 001 -2 010 -1 011 100 1 101 2 110 3 111 4 B = 127 p x -127 128 -126 127 9 A bias value, B, is usually chosen to be roughly in the middle of the range. The actual value x of a bit pattern p is x = p - B. Given x, the bit pattern should be p = x + B.

2 CSCI206 - Computer Organization & Programming
Logical Operations, Making Decisions zyBook: 5.6, 5.7

3 MIPS Integer Operations
Bit-wise!

4 Bitwise/logical Instructions
Category Instruction C operator MIPS Instruction Logical shift left << sll shift right >> srl bitwise AND & and, andi bitwise OR | or, ori bitwise XOR ^ xor, xori

5 Shift Left - Machine Code
r-type instruction with an immediate! sll $s1, $s0, 8 shift left 16 17 8 opcode rs rt rd shift amount function unused (0) 0x00108A00 R[rd] = R[rt] << shamt

6 Comparison and Decision Instructions
slt rd, rs, rt ;set less than: R[rd] = (R[rs] < R[rt]) ? 1 : 0 slti rt, rs, imm ;set less than immediate ;R[rt] = (R[rs] < imm) ? 1 : 0 beq r1, r2, label ; branch to label if r1 == r2 bne r1, r2, label ; branch to label if r1 != r2

7 Common Programming Patterns in C
Decisions if (boolean condition){ (consequent) }else{ (alternative) } switch (var){ case 0: //case_0_code; break; case 1: //case_1_code; case 2: //case_2_code; default: //default_code; Loops do{ // Code for the loop's body // goes here. }while(condition) while(condition) { } for(initialization; condition; update){ // Code for the for loop's body

8 IF lw $t1, 0($s0) ;load a from mem // assume a is an int
if (a == 1) { // consequent code }else{ // alternative code } lw $t1, 0($s0) ;load a from mem addi $t2, $zero, 1 ;set $t2 to 1 IF: beq $t1, $t2, A ; alternative code j ENDIF A: ; consequent code ENDIF: lw $t1, 0($s0) ;load a from mem addi $t2, $zero, 1 ;set $t2 to 1 IF: bnq $t1, $t2, B ; consequent code j ENDIF B: ; alternative code ENDIF:

9 If activity if ($a0 > 127){ $v0 = 1; } else { $v1 = 1; }

10 if ($a0 > 127){ $v0 = 1; } else { $v0 = 0; } If solution

11 DO/WHILE addi $t0, $zero, 15 x = 15; do_begin: do{ ; Loop body
// Code for the loop’s body // goes here. x = x - 1 } while(x >= 10) addi $t0, $zero, 15 do_begin: ; Loop body addi $t0, $t0, -1 slti $t1, $t0, 10 beq $t1, $zero, do_begin do_end:

12 WHILE/DO addi $t0, $zero, 15 while_begin: x = 15 slti $t1, $t0, 10
beq $t1, $zero, while_end ; LOOP BODY addi $t0, $t0, -1 j while_begin while_end: x = 15 while(x >= 10){ // Code for the loop’s body // goes here. x = x - 1 }

13 While activity $v0 = 0; while ($a0 > 0){ $v0 ^= 1;
$a0 = $a0 & ($a0 - 1); }

14 While solution $v0 = 0; while ($a0 > 0){ $v0 ^= 1;
$a0 = $a0 & ($a0 - 1); } While solution

15 FOR for(i = 0; i < 10; i ++){ addi $t0, $zero, 0
// Code for the for loop's body // goes here. } addi $t0, $zero, 0 for_begin: slti $t1, $t0, 10 beq $t1, $zero, for_end ; LOOP BODY addi $t0, $t0, -1 j for_begin for_end:

16 For activity $v0 = 0; for ( $t0 = 0; $t0 < 32; $t0++ ){
if ( ($a0 >> $t0) & 0x1 == 0x1 ){ $v0 ^= 1; }

17 For solution

18 SWITCH switch (var){ case 0: //case_0_code; break;
default: //default_code; } BEQ var, 0, CASE_0 BEQ var, 1, CASE_1 BEQ var, 2, CASE_2 default: ; default_code j END CASE_0: ; case_0_code CASE_1: ; case_1_code CASE_2: ; case_2_code END:

19 SWITCH (jump table) .data TABLE: .word CASE_0 .word CASE_1
.text sll $t1, var, 2 lw $t0, TABLE($t1) jr $t0 CASE_0: ; case_0_code j END CASE_1: ; case_1_code CASE_2: ; case_2_code END: switch (var){ case 0: //case_0_code; break; case 1: //case_1_code; case 2: //case_2_code; } Warning: this assumes all cases are used (starting with zero) and the input is valid!

20 Pseudo branch instructions
The assembler uses the $at register and slt to implement blt (branch less than) bgt (branch greater than) ble (branch less than or equal) bge (branch greater than or equal)

21 Pseudo Branch Example blt $t0, $t1, label slt $at, $t0, $t1
pseudo-instruction if $t0 < $t1 j label blt $t0, $t1, label if $t0 < $t1 $at = 1; else $at = 0; if $at != 0 j label slt $at, $t0, $t1 bne $at, $zero, label true-instruction


Download ppt "How to represent signed integers"

Similar presentations


Ads by Google