CS401 Short Notes For Midterm (Study With BRD)
CS401 Short Notes For Midterm (Study With BRD)
com
ASSEMBLY LANGUAGE PROGRAMMING
m (CS401)
SUBJECTIVE SOLVED FOR MID TERM
num1: dd 40000
shl word [num1], 1
rcl word [num1+2], 1
The DD directive reserves a 32bit space in memory; however the value we placed there will fit
in 16bits. So we can safely shift the number left 16 times.
The least significant word is accessible at num1 and the most significant word is accessible at
num1+2.
The two instructions are carefully crafted such that the first one shifts the lower word towards
the left and the most significant bit of that word is dropped in carry. With the next instruction we push that
dropped bit into the least significant bit of the next word effectively joining the two 16bit words.
The final carry after the second instruction will be the most significant bit of the higher word,
which for this number will always be zero.
Is it necessary to provide the segment and offset address in case of FAR jump ?
Segment and offset must be given to a far jump. Because, sometimes we may need to go from
one code segment to another, and near and short jumps cannot take us there. Far jump must be used and a two
byte segment and a two byte offset are given to it. It loads CS with the segment part and IP with the offset part.
Explain MUL instruction in both cases (i) if the source operand is byte (ii) if the source operand is
a word
MUL performs unsigned multiplication of the source operand and the accumulator. In the case
of source operand is a byte, then it is multiply by the register AL and the double length result will be return into
AH and AL. On the other side if the source operand is a word, then it is multiplied by the register AX, and the
double-length result is returned in registers DX and X.
Purpose of INT 1
Int 1 is Single step Interrupt, This interrupt is used in debugging with trap Flag. If the trap flag
is set the single step interrupt is generated after every instructions.
These are instructions that control the program execution and flow by
playing with the instruction pointer and altering its normal behavior to point
to the next instruction. Some examples are:
cmp ax, 0
jne 1234
6 www.vuanswer.com
We are changing the program flow to the instruction at 1234 address if the
condition that we checked becomes true. m
dec cl ; decrement bit count
jnz checkbit ; repeat if bits left
q=18…RET instruction…
ROR instructions….
In the rotate right operation every bit moves one position to the right and the bit dropped from
the right is inserted at the left. This bit is also copied into the carry flag. The operation can be understood by
imagining that the pipe used for shifting has been molded such that both ends coincide. Now when the first ball
is forced to move forward, every ball moves one step forward with the last ball entering the pipe from its other
end occupying the first ball’s old position. The carry basket takes a snapshot of this ball leaving one end of the
pipe and entering from the other.
…push function..
The push operation copies its operand on the stack, When an item is pushed on a decrementing
stack, the top of the stack is first decremented and the element is then copied into this space.
INT 4……….
INT 4, Arithmetic Overflow, change of sign bit .The overflow flag is set if the sign bit unexpectedly
changes as a result of a mathematical or logical instruction. However the overflow flag signals a real overflow
only if the numbers in question are treated as signed numbers. So this interrupt is not automatically generated
but
as a result of a special instruction INTO (interrupt on overflow) if the overflow flag is set. Otherwise the INTO
instruction behaves like a NOP (no operation).
CMPS
CMPS subtracts the source location DS:SI from the destination location ES:DI. Source and
Destination are unaffected. SI and DI are updated accordingly. CMPS compares two blocks of memory for
equality or inequality of the block. It subtracts byte by byte or word by word. If used with a REPE or a REPNE
prefix is repeats as long as the blocks are same or as long as they are different. For example it can be used for
find a substring. A substring is a string that is contained in another string. For example “has” is contained in
“Mary has a little lamp.” Using CMPS we can do the operation of a complex loop in a single instruction. Only
the REPE and REPNE prefixes are meaningful with this instruction.
A decrementing stack moves from higher addresses to lower addresses as elements are added in it while an
incrementing stack moves from lower addresses to higher addresses as elements are added.
As the 8088 stack works on word sized elements. Single bytes cannot be pushed or popped from the stack.
The direction of movement is controlled with the Direction Flag (DF) in the flags register. If this flag is cleared
DF=0, the direction is from lower addresses towards higher addresses and if this flag is set DF=1, the direction
is from higher addresses to lower addresses. If DF is cleared, DF = 0 this is called the autoincrement mode of
string instruction, and if DF is set, DF=1, this is called the autodecrement mode. There are two instructions to
set and clear the direction flag.
scrollup: push bp
mov bp,sp
push ax
push cx
push si
push di
push es
push ds
mov ax, 80 ; load chars per row in ax
mul byte [bp+4] ; calculate source position
mov si, ax ; load source position in si
push si ; save position for later use
shl si, 1 ; convert to byte offset
mov cx, 2000 ; number of screen locations
sub cx, ax ; count of words to move
mov ax, 0xb800
8 www.vuanswer.com
mov es, ax ; point es to video base
mov ds, ax m
; point ds to video base
xor di, di ; point di to top left column
cld ; set auto increment mode
rep movsw ; scroll up
mov ax, 0x0720 ; space in normal attribute
pop cx ; count of positions to clear
rep stosw ; clear the scrolled space
pop ds
pop es
pop di
pop si
pop cx
pop ax
pop bp
ret 2
Using our basic shifting and rotation instructions we can effectively shift a 32bit number in memory word by
word. We cannot shift the whole number at once since our architecture is limited to word operations. The
algorithm we use consists of just two instructions and we name it extended shifting.
num1: dd 40000
shl word [num1], 1
rcl word [num1+2], 1
The DD directive reserves a 32bit space in memory; however the value we placed there will fit in 16bits. So we
can safely shift the number left 16 times.
The least significant word is accessible at num1 and the most significant word is accessible at num1+2.
The two instructions are carefully crafted such that the first one shifts the lower word towards the left and the
most significant bit of that word is dropped in carry. With the next instruction we push that dropped bit into the
least significant bit of the next word effectively joining the two 16bit words.
The final carry after the second instruction will be the most significant bit of the higher word, which for this
number will always be zero.
In a string instructions, block have a start and end. Instructions can work from the start toward to end
and from the end towards start. They can work in both directions and the have to be allowed to work
in both directions; otherwise some operations with overlapping blocks become not possible.
When the parameters by pushed for subroutine are waste after the subroutine return. They must be
clear from the stack be caller and the callee.
The REP allow instructions to operate on a number of data elements in a one instructions, REPE
repeats the instructions string instructions while the zero flash is set and REPNE repeat while not
equal or repeat the instruction while the zero flag is not set
1. SCAS instructions has variety of functions it compare a source of byte or word in register AL or
AX with destination string address by ES:DI and updating flags.
2. It is used to locate equality or in-equality in a string
3. SCAS is bit different from the other instructions.
4. REPE and REPNE are used with this type of instructions
5. This is more like CMP instructions that it does subtraction of the operands.
Local variables are created by STACK and these variables are needed when subroutine are in
execution and not afterwards. They do not take place like global variables. They are temporary not
permanent. Local variables are created when the subroutine is call. There meaning is in subroutine
not outside. There most convenient place is stack. Special manipulation is needed for this task.
Same base pointer can be used to access the local variables.
11 www.vuanswer.com
m
REP prefix use with LODS is not meaningful as only last value loaded will be remaining in register. It
is use in loop paired with STOS
Not operator inverts the bits of byte or word operand. This is single operand instructions. And invert
the result into 1’s complement form.
Out of line procedures in the temporary division, the concept of the round about near calls are called
Intra Segment. On the other hand far calls are called inter-segment class.
The test instruction is used for bit testing. BX holds the mask and in every next iteration it is shifting
left, as our concerned bit is now the next bit.
LES and LDS load a segment register and a general purpose register from two consecutive memory
locations. LES loads ES while LDS loads DS. Instructions has two parameters, one is the general
purpose register to be loaded and the other is the memory location from which to load these
registers. There major application of these instructions is when a subroutine receives a segment
offset pair as an argument and the pair is to be loaded in a segment and an offset register.
The MOVS instruction is a byte transfer or word from the source location D:SI to the destination. ES:
DI and updated SI and DI to point to the next locations. It is use to moave block of memory. Dif plays
important role in case of overlapping blocks And CMPS instructions subtracts the source location DS:
SI from the destination location ES: DI. Source and the destination are unaffected.
12 www.vuanswer.com
Explain MUL instruction in both cases (i) if the sourcem
operand is byte (ii) if the source operand is a
word
MUL performs un unsigned mulitiplication of the source operand and the accumulator. In the case of
source operand is a byte, then it is multiply by the register AL and the double length result will be
return into AH and AL. On the other side if the source operand is a word, then it is mulitiplied by the
register AX, and the double-length result is returned in registers DX and X.
We can control direction with Direction Flag (DF) in the flag Register. If the DF is value with it will be
auto increment. and if the flag Register DF=1 it can set auto decrement.
Purpose of INT 1
Int 1 is Single step Interrupt, This interrupt is used in debugging with trap Flag. If the trap flag is set
the single step interrupt is generated after every instructions.
These are instructions that control the program execution and flow by
playing with the instruction pointer and altering its normal behavior to point
to the next instruction. Some examples are:
cmp ax, 0
jne 1234
We are changing the program flow to the instruction at 1234 address if the
condition that we checked becomes true.
q=18…RET instruction…
…push function..
INT 4……….
INT 4, Arithmetic Overflow, change of sign bit
The overflow flag is set if the sign bit unexpectedly changes as a result
of a mathematical or logical instruction. However the overflow flag
signals a real overflow only if the numbers in question are treated as
signed numbers. So this interrupt is not automatically generated but
as a result of a special instruction INTO (interrupt on overflow) if the
overflow flag is set. Otherwise the INTO instruction behaves like a NOP
(no operation).
CMPS
CMPS subtracts the source location DS:SI from the destination location
ES:DI. Source and Destination are unaffected. SI and DI are updated
accordingly. CMPS compares two blocks of memory for equality or inequality
of the block. It subtracts byte by byte or word by word. If used with a REPE
or a REPNE prefix is repeats as long as the blocks are same or as long as
they are different. For example it can be used for find a substring. A
substring is a string that is contained in another string. For example “has” is
contained in “Mary has a little lamp.” Using CMPS we can do the operation of
a complex loop in a single instruction. Only the REPE and REPNE prefixes
are meaningful with this instruction.