Write the programs in Assembly Lanuage that implement the following programming
ID: 1715418 • Letter: W
Question
Write the programs in Assembly Lanuage that implement the following programming structures. Given the following variables and initial values: A =10; B =15; C =0; D =100; E =5; F =20; G =3; H =99; J=25; K=1; L=50; M=3; N=4; Jle LI C- A-B move CO End IF b) Begin _IF_THEN ELSE If A2 B then C:- A-B else C= B-A; End_IF_THEN ELSE c) Begin_While While D 20 do F:= F + 1; End _While d) Begin Repeat Repeat H:-H- G Until HSG End Repeat e) Begin_For For I: -1 to 10 do J J-1; End For Begin For For I:-13:22 do Begin Mr. J+ 1; End ForExplanation / Answer
a)
CMP ax, bx; %compares conetns of ax and bx
JNZ label; % JNZ means jump if not zero. If contents of ax>bx, then jump to instruction labeled as label
label: SUB ax,bx; % subtract contents of ax and bx and store result in ax
MOV cx,ax; % move contents of ax in cx
end;
Note: In program above i have specifically used jump instruction, although result it could have been done by other ways. This is because this part of program will be actually a part of bigger program jumps have to be used necessarily.In other words , in real programs, there will be lot of code in between compare instruction and subtract instruction.
CMP instruction is non destructive AND operation. It doesnt change contents of register, but only sets flags based on results.
b)
CMP ax, bx; %compares conetns of ax and bx
JNZ label; % JNZ means jump if not zero. If contents of ax>bx, then jump to instruction labeled as label
SUB bx,ax; % if ax<bx above instrction will be skipped as this one will be executed
MOV cx,ax; % stores result of subtraction done in above instruction in cx
end
label: SUB ax,bx; % subtract contents of ax and bx and store result in ax, if contents of ax>bx.
MOV cx,ax; % move contents of ax in cx
end;
c)
mov dx,100;
mov ex, 5;
mov fx,20; % assume ex and fx are registers. You can use any other registers as well to store the variables
label1: CMP dx,FFFF;
%compares (ANDs) contents of dx with FFFF. FFFF is in HEX. In binary FFFF is all 1's. So, carry flag will be zero only when dx has all zeroes, since CMP is bitwise non destructive AND.
JNZ label; % If carry flag is not zero, jump to label
label : SUB dx,ex; % subtract contents of dx and ex and store in dx
INC f; % increment fx by 1.
JMP label1: % Jump to label1 again for checking he condition
end;
d)
MOV ax,3; % stored variable G in ax
MOV bx,99; % stored variable H in bx
label: sub bx,ax; % subtracts contents of ax from bx
JG label; % if result of subtract of bx and ax is positive (JG stands for jump if greater), jump to label (subtract again),otherwise to end.
end;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.