0% found this document useful (0 votes)
29 views

Session 3 and 4

Uploaded by

Aditya Agarwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Session 3 and 4

Uploaded by

Aditya Agarwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

1

Course Code : CSE 2151


Credits : 04
▪ Give a short sequence of machine instructions for the task
▪ Add the contents of memory location A to those of location B, and place the answer in location C
▪ Following instructions are the only instructions available to transfer data between the memory
and the general-purpose registers.
▪ Load Ri, LOC

▪ Store Ri, LOC


▪ Do not change the contents of either location A or B.

▪ Solution:
▪ Load R3, A
▪ Load R4, B
▪ Add R5, R3, R4
▪ Store R5, C

2
▪ Number representation
▪ Decimal number system:
▪ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
▪ Each digit has a position value in terms of powers of 10
▪ 123= 1 * 102 + 2 * 101 + 3 * 100
▪ Binary number system
▪ 0, 1
▪ Each digit has a position value in terms of powers of 2
▪ 101= 1 * 22 + 0 * 21 + 1 * 20
▪ n-bit vector B = bn−1 . . . b1b0,
▪ where bi = 0 or 1 for 0 ≤ i ≤ n − 1
▪ unsigned integer value V(B) in the range 0 to 2n − 1, where
▪ V(B) = bn−1 × 2n−1 +· · ·+b1 × 21 + b0 × 20
3
▪ Unsigned integer
▪ If the integers are represented using 4 bit
▪ 0(10) in binary?
0000
▪ 15(10) in binary?
1111 = 1 * 23 + 1 * 22 + 1 * 21 + 1 * 20

▪ Signed integer

4
▪ Unsigned integer
▪ If the integers are represented using 4 bit
▪ 0(10) in binary?
0000
▪ 15(10) in binary?
1111 = 1 * 23 + 1 * 22 + 1 * 21 + 1 * 20

▪ Signed integer
▪ Sign and magnitude
▪ One’s complement
▪ Two’s complement

▪ In all three systems,


▪ the positive numbers have the same bit representation
▪ the negative numbers have different bit representation
▪ positive numbers - the leftmost bit is 0
▪ negative numbers- the leftmost bit is 1
5
▪ Negative values - most significant bit of the Positive Value b3 b2b1b0 b3 b2b1b0 Negative Value
corresponding positive value changed from 0 to 1 1111 -7
+7 0111
▪ Sign- MSB
+6 0110 1110 -6
▪ Magnitude (or number)- remaining bits
+5 0101 1101 -5
▪ 0 represented as positive and negative
+4 0100 1100 -4

+3 0011 1011 -3

+2 0010 1010 -2

+1 0001 1001 -1

+0 0000 1000 -0

6
▪ Negative values - complementing each bit in Positive Value b3 b2b1b0 b3 b2b1b0 Negative Value
the corresponding positive value +7 0111 1000 -7
▪ Negative to positive- complementing each bit 1001 -6
+6 0110
in the corresponding negative value
+6 0110 +5 0101 1010 -5

-6 1001 +4 0100 1011 -4

▪ For n-bit numbers, this operation is equivalent +3 0011 1100 -3


to subtracting the number from 2n − 1.
+2 0010 1101 -2
▪ Example: +6 to -6 in a 4-bit representation
+1 0001 1110 -1
▪ 2n-1 =15 1111
+6 0110 +0 0000 1111 -0
-6 1001
▪ 0 represented as positive and negative

7
▪ 2’s-complement of an n-bit number is done by
First Value Second Value Difference
subtracting the number from 2n.
0 0
▪ How to subtract 2 numbers? 0

1
▪ 0101 – 0100 0 1
( by borrowing)
▪ 001 1 0 1

▪ 01100 – 01000 1 1 0
▪ 0100

▪ 010000 – 0101
▪ 01011

▪ 01000 – 0011
▪ ?

8
▪ Negative values - adding 1 to the 1’s- Positive Value b3 b2b1b0 b3 b2b1b0 Negative Value
complement of corresponding positive value +7 0111 1000 -8
▪ Example: +6 to -6 in a 4-bit representation 1001 -7
+6 0110
▪ 2n-1 =15 1111
+5 0101 1010 -6
+6 0110 conversion using
1001 1’s complement +4 0100 1011 -5
+1 0001 1100 -4
+3 0011
-6 1010
+2 0010 1101 -3
▪ For n-bit numbers, this operation is equivalent
1110 -2
to subtracting the number from 2n. +1 0001

1111 -1
▪ Example: +6 to -6 in a 4-bit representation +0 0000
▪ 2n 10000
+6 00110
-6 01010
▪ 0 represented as positive 9
▪ 4 bits: -8 to +7
-24-1 to +24-1-1
▪ 5 bits: -16 to +15
-25-1 to +25-1-1
▪ 6 bits: -32 to +31
-26-1 to +26-1-1
▪ n bits: -2n−1 to +2n−1 − 1

10
▪ For 4-bit numbers, the value −8 is
representable in the 2’s-complement
system but not in the other systems.
▪ Sign-and-magnitude system seems the
most natural
▪ 1’s-complement system is easily related to
this system
▪ 2’s-complement appears unusual.
▪ However, it leads to the most efficient way to
carry out addition and subtraction operations.
▪ It is the one most often used system in
modern computers.

11
▪ #include <iostream>
using namespace std;
int main() {
unsigned short x=65535, y=65537;
cout<<x<<" "<<y<<endl;
return 0;
}
▪ x=65535, y=1
▪ unsigned short: 16 bits
▪ 0 to 65535 (0 to 2n-1)
▪ If the value is out of range, it is divided by largest_number+1 of that datatype, and
only the remainder kept.
▪ Here, 65537 % 65536= 1
▪ Any number bigger than the largest number, “wraps around” the largest number in
that type.
12
▪ #include <iostream>
using namespace std;
int main() {
unsigned short x=0, y=-1;
cout<<x<<" "<<y<<endl;
return 0;
}
▪ x=0, y= 65535
▪ wraps around to the top of the range.

13
▪ 7 (0111) + 5 (0101) = ?
▪ From 7 move 5 units in clockwise direction
▪ 12

▪ 9 + 14=?
▪ From 9 (1001) move 14 units in clockwise direction
▪ 7

14
▪ 2-bit addition:

▪ Multiple bit addition


▪ Similar to decimal addition method
▪ add bit pairs starting from the low-order end of the bit vectors, transmitting the carries
toward the high-order end
▪ The carry-out from the previous bit pair becomes the carry-in to the current bit pair
▪ The carry-out from the bit pair in the right must be added to the current bit pair to
generate the sum and carry-out at that position
▪ If both bits of a pair are 1 and the carry-in is 1, then the sum is 1 and the carry-out is 1

15
▪ #include <iostream>
using namespace std;
int main(){
unsigned int x=1, y=2;
cout<<x-y;
return 0;
}
▪ 4294967295
▪ This occurs due to -1 wrapping around to a number close to the top of the range of a 4-byte
integer
▪ 232-1➔ 4294967296-1
▪ 32 bits = 4 bytes (size of int)

▪ Hence, subtraction is not recommended.


16
▪ Textbook 1:
▪ Chapter 1: 1.4.1

17

You might also like