Uploaded by BSK KEITH

Doc2

advertisement
What is the value of the C status flag after the following sequence: mov al, 3 add al, 4 b. What is
the value of the C status flag after the following sequence: mov al, 3 sub al, 4
ANSWER
mov al,3
add al,4
in addition carry flag will always be set if the result produces an extra bit.
here the result is 7 which does not produce an extra bit. hence the status of carry bit is zero.
NOW,
mov al , 3
sub al , 4
carry flag will be set if the magnitude of the subtrahend is greater than minuend.
so in this operation the carry set is set to be '1'.
The xchg instruction can be used to exchange the contents of two registers. Suppose that the
x86 instruction set did not support this instruction. (A). Implement xchg ax, bx using only push
and pop instructions. (B). Implement xchg ax, bx using only the xor instruction (do not involve
other registers).
ANSWER
values in bx and ax should be interchanged.
(a)
PUSH AX,
PUSH BX,
POP AX,
POP BX
(b)
using XOR:ax= ax xor bx
bx=ax xor bx
ax=ax xor bx
List some reasons why it is worthwhile to study assembly language programming.
It shows how data is represented in memory
It clarifies the execution of instructions.
It is necessary to understand the behavior of your machine.
What is an assembly language?
Assembly language is programming language that is one step away from machine
language. Assembly language includes symbolic names for locations. It also includes
directives and macros.
Consider the following C program:
/* a simple C program to average 3 integers */
main () {
int avg;
int i1 = 20;
int i2 = 13;
int i3 = 82;
avg = (i1 + i2 + i3)/3;
}
Write an MASM version of this program.
MASM CODE :
; Data initialization
DATA SEGMENT
I1 DB 20
I2 DB 13
I3 DB 82
AVG DB ?
ENDS
CODE SEGMENT
ASSUME DS:DATA CS:CODE ; Data name given for data segment and code ;for code segment
START:
MOV AX, DATA ; initialization of data segment
MOV DS, AX ; move data from AX to DS
MOV AL, I1 ; move I1 to AL
ADD AL, I2 ; Add I1 and I2, result is in AL
ADD AL, I3 ; Add AL and I3, result is in AL
MOV AH, 0
MOV DL, 3 ; move 3 to DL register as code uses 3 values
IDIV DL ; Divide AX register with 3.
MOV AVG, AL ; result of division is in register AL and is
; moved to variable AVG
MOV AH, 4CH
INT 21H
; exit DOS interrupt.
ENDS
END START
The initialize data directives can be used to initialize multiple locations. For example, db 0x55,0x56,0x57
reserves three bytes and initializes their values. NASM supports the special token $ to allow calculations
to involve the current as sembly position. That is, $ evaluates to the assembly position at the beginning
of the line containing the expression. With the preceding two facts in mind, consider the following
sequence of directives: message db ‘hello, world’ msglen equ $-message What value is assigned to the
symbol msglen?
Answer :
Hello -5
Comma (,) -1
Space -1
World -5
So Total is 5+1+1+5 = 12
Assume the three symbolic variables V1, V2, V3 contain integer values. Write an NASM code fragment
that moves the smallest value into integer ax. Use only the instructions mov, cmp, and jbe.
ANSWER)
NASM code fragment to move smallest value to register AX:
V1: RESW
V2 RESW
V3: RESW
MAIN CODE:
MOV AX, V1 ; load V1 to register AX
(Accumulator)
CMP AX, V2 ; if AX is less than or equal to V2
then
JBE LOOP1 ; jump to LOOP1
MOV AX, V2 ; else move V2 to AX
LOOP1: CMP AX, V3 ; if AX is less than or equal to V3
then
JBE LOOP2 ; jump to LOOP2
MOV AX, V3 ; else AX holds V3
LOOP2:
Q. Given NASM code fragment,
mov al, 0
cmp al, al
je next
Explanation:
In the instruction “0” is moved to the register “AL”, in second instruction “0” is compared with “0”, in
third instruction if “0” is equal to “0”, then jumps to loop next. All these three instructions is equal to
“JMP Next” instruction.
step: 3 of 3
Thus, “JMP Next” is the single equivalent instruction
Consider the following C code fragment:
if (EAX == 0) EBX = 1; else EBX = 2;
Write an equivalent NASM code fragment.
main:
push
mov
mov
mov
mov
mov
mov
add
rbp
//start of main
rbp, rsp
DWORD PTR [rbp-4], 20 //assign i1 variable
DWORD PTR [rbp-8], 13 //assign i2 variable
DWORD PTR [rbp-12], 82 //assign i3 variable
edx, DWORD PTR [rbp-4] //move i1 to approp registers
eax, DWORD PTR [rbp-8] //move i2 to approp registers
edx, eax
// add i1+i2 first and store it in
edx
mov
add
movsx
imul
multiplication
shr
sar
mov
mov
sub
mov
mov
pop
ret
eax,
eax,
rdx,
rdx,
DWORD PTR [rbp-12] //move i3 to approp register
edx
// add result of prev addition + i3
eax
// move the result to approp register
rdx, 1431655766
// dividing the result in terms of
rdx, 32
eax, 31
ecx, eax
eax, edx
eax, ecx
//deallocate the registers
DWORD PTR [rbp-16], eax
eax, 0
rbp
//end of main
In the following program, assume that a, b, x, y are symbols for main memory loca tions.
What does the program do? You can answer the question by writing the equiva lent logic
in C. mov eax,a mov ebx,b xor eax,x xor ebx,y or eax,ebx jnz L2 L1: ;sequence of
instructions… jmp L3 L2: ;another sequence of instructions… L3:
int a, b, x, y; // main memory symbols
void fun() {
// res1 reperesent eax
// and res2 represent ebx
int res1 = a;
int resb = b;
a = a ^ x;
b = b ^ y;
a = a | b;
if (a == 0) {
// L1 instructions
}
else {
// L2 instructions
}
// L3 execution
}
Consider the following NAMS instruction: cmp vleft, vright For signed integers, there are
three status flags that are relevant. If vleft = vright, then ZF is set. If vleft 7 vright, ZF is unset
(set to 0) and SF = OF. If vleft 6 vright, ZF is unset and SF ≠ OF. Why does SF = OF if vleft 7
vright?

Sign Flag (SF) − It shows the sign of the result of an arithmetic operation. This flag is
set according to the sign of a data item following the arithmetic operation. The sign
is indicated by the high-order of leftmost bit. A positive result clears the value of SF
to 0 and negative result sets it to 1.
 Overflow Flag (OF) − It indicates the overflow of a high-order bit (leftmost bit) of data
after a signed arithmetic operation.
This was reason for both to be equal i.e to 0 when vleft > vright
List some disadvantages of assembly language compared to high-level languages?
Development time. Writing code in assembly language takes much longer time than in
a high level language.
Reliability and security. It is easy to make errors in assembly code.
Compilers have been improved a lot in recent years
Debugging and verifying. Assembly code is more difficult to debug and verify because
there are more possibilities for errors than in high-level code

List some advantages of assembly language compared to high-level languages?
Function libraries: The total benefit of optimizing code is higher in function libraries
that are used by many programmers.
Making compilers: Understanding assembly coding techniques is necessary for
making compilers, debuggers and other development tools.
Hardware drivers and system code: Accessing hardware, system control registers
etc. may sometimes be difficult or impossible with high level code.



Download