in Programming recategorized by
3,942 views
21 votes
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;
in Programming recategorized by
3.9k views

2 Comments

This is a good exercise to practice arithmetic operations using Hex and Binary values, esp. to try out Booth's Algo.
1
1
    Z               :unsigned integer, (each integer is represented by two bytes)

should be :

    Z               :unsigned integer; (each integer is represented by two bytes)

the semi-colon instead of comma for the program to be consistent with the constructs. Otherwise error might be the answer.

0
0

3 Answers

33 votes
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 }$

edited by

4 Comments

edited by
$W$ in decimal $=  -54$

$2’s$ complement  of $54$ $= (1’s$ complement of $54) +1$

                                 $=1’s$ complement of $00110110+ 1$

                                  $= 11001001 +1$

                                  $= 11001010$ $ ( CA_{H})$
0
0

How to perform abs value subtraction of two binary no.s without converting them to decimal? @ASNR1010

0
0

@Abhrajyoti00

do normal subtraction just take the borrow  = base.

1
1
11 votes
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$
edited by

1 comment

Subtraction will set the borrow.Does this count as overflow as no bit reserved is there for borrow?
1
1
–2 votes
–2 votes

i think X = EC
          W =52 
           Z = will be error because its exceeds the size...

 

Related questions