Dark Mode

3,942 views

21 votes

Consider the following high level programming segment. Give the contents of the memory locations for variables $W, X, Y$ and $Z$ after the execution of the program segment. The values of the variables $A$ and $B$ are $5CH$ and $92H$, respectively. Also indicate error conditions if any.

var A, B, W, X, Y :unsigned byte; Z :unsigned integer, (each integer is represented by two bytes) begin X :=A+B Y :=abs(A-B); W :=A-B Z :=A*B end;

1

33 votes

Best answer

The maximum value that can be accommodated in an unsigned byte $= 255$ and unsigned int $= 65535.$

$A$ and $B$ are given in Hexadecimal.

- $A = 5C_{H } = (92)_{10 }$
- $B = 92_{H } = (146)_{10}$
- $X = A + B = (238)_{10} = EE_{H }$
- $Y = \text{abs} (A - B) = (54)_{10 } = 36_{H }$
- $W = A - B = (-54)_{10 }$

Negative numbers represented in $2$'s complement form $\implies -54 = 11001010$ ( in $8$-bit representations )

But $W$ is unsigned, therefore it cannot look for the sign $\implies W = 11001010 = CA_{H }$

$Z = A \ast B = (13432)_{10 } = 3478_{H }$

11 votes

There won't be any problem.

The quick approach is:---

Here, $H$ denotes Hexadecimal No.

$X=A+B=5CH+92H=EEH$ (One Byte is enough)

$Y=abs(A-B) \ \& \ W=A-B$

(Subtraction operation never cause Overflow So $1$ Byte is enough)

$Z=A*B$

$n -bit * n-bit$ requires $2n \ bit$.

So, $8-bit*8-bit =16 \ bit$ required ($\&$ as per question $z$ is $16 \ bit$ unsigned number)

Max value that can be accommodated inside an unsigned $byte = 255$ and unsigned $int = 65535$

The quick approach is:---

Here, $H$ denotes Hexadecimal No.

$X=A+B=5CH+92H=EEH$ (One Byte is enough)

$Y=abs(A-B) \ \& \ W=A-B$

(Subtraction operation never cause Overflow So $1$ Byte is enough)

$Z=A*B$

$n -bit * n-bit$ requires $2n \ bit$.

So, $8-bit*8-bit =16 \ bit$ required ($\&$ as per question $z$ is $16 \ bit$ unsigned number)

Max value that can be accommodated inside an unsigned $byte = 255$ and unsigned $int = 65535$