In: Computer Science
What is the function of this program (what does it do)? What is the output if the input is 12d (12 decimal)? What would be the output if the input is 47d?
Sub bx, bx
Mov ecx, 16
GetInt ax
Nxt:
shl ax,1
jc Addone
jnc LB1
Addone: inc bx
LB:
loop Nxt
PutInt bx
sub bx,bx //load bx with 0
move ecx,16 //load register ecx with 16
getint ax //read integer value to register ax
Nxt: shl ax,1 //shift left the ax content by 1
jc Addone //if there is carry, then goto label addone
jnc LB1 //if no carry,then goto label LB1
Addone: inc bx //increment bx
LB: loop Nxt //loop to lable Nxt
putint bx //print the value of bx
This program read an integer into the register ax and shift it left by 1 bit at a time. If the shift operation loads a 1 in the carry flag, count it into the register bx and repeat it in a loop. So finally the register bx will have the number of 1s present in the given input.
Answers:
1. Function of the program: This program counts the number of 1s in the given number.
2. When input is 12d: Displays 2
The program outputs the number of 1s in the equivalent hexadecimal value of the given number. ie it displays 2 since the hex equivalent of 12d is C which is 1100 and has 2 1s in it.
3.When input is 47d: Displays 5
The program outputs the number of 1s in the equivalent hexadecimal value of the given number. ie it displays 5 since the hex equivalent of 47d is 2F which is 0010 1111 and has 5 1s in it.