In: Computer Science
Please show full work and give explanation to be upvoted.
11. How does a program determine whether or not to jump based off of a comparison?(In assembly language)
12. Left shift 15 by 3
11.
Conditional execution in assembly language is defined by a few branching and looping instructions. These instructions can change the progression of control in a program.
Syntax : CMP destination, source
The CMP instruction compares two operands. It is generally used in conditional execution. This instruction basically subtracts one operand from the other for comparing whether the operands are equal or not. It does not disturb the destination or source operands. It is used along with the conditional jump instruction for decision making. CMP is often used for comparing whether a counter value has reached the number of times a loop needs to be run. Consider the following typical condition
Unconditional Jump - The JMP instruction provides a label name where the flow of control is transferred immediately.
Syntax : JMP label
Example:
MOV AX, 00 ; Initializing AX to 0 MOV BX, 00 ; Initializing BX to 0 MOV CX, 01 ; Initializing CX to 1 L20: ADD AX, 01 ; Increment AX ADD BX, AX ; Add AX to BX SHL CX, 1 ; shift left CX, this in turn doubles the CX value JMP L20 ; repeats the statements
Conditional Jump - If some specified condition is satisfied in conditional jump, the control flow is transferred to a target instruction.
Example:
INC EDX CMP EDX, 10 ; Compares whether the counter has reached 10 JLE LP1 ; If it is less than or equal to 10, then jump to LP1
LP1: ....
Assembly code:
include 'emu8086.inc'
.model small
.stack 100h
.data
.code
main proc
mov dl,5
mov bl,7
cmp dl,bl
je show
print 'Both are not same'
mov ah,04h
int 21h
show:
print 'Both are same'
mov ah,04h
int 21h
main endp
end main
output:
12.
In a left shift the binary value shifts to left by 1 place. Actually each shift is a 2 multiple of the value. So the 15 (00001111) multipled by 8 (2*2*2) , will calculate 120 (01111000) as the value whose binary form is the left shift of 15 by 3.
Syntax Binary form Value
x=15; 00001111 15
x= x>>3; 01111000 15 *(2*2*2) = 120
left shift of 15 (00001111) by 3 is 01111000 .