SlideShare a Scribd company logo
Programming in Assembly Language
Memory Segmentation
ī‚ˇ Memory segments are a direct consequence of using a 20 bit address in a 16 bit processor
ī‚ˇ Memory is partitioned into 64K (216
) segments
ī‚ˇ Each segment is identified by a 16-bit segment number ranging from 0000h-FFFFh
ī‚ˇ Within a segment, a memory location is specified by a 16-bit offset (the number of bytes from the
beginning of the segment)
ī‚ˇ The Segment:Offset address is a logical address
Segment:Offset Addresses
ī‚ˇ A4FB:4872h means offset 4872h within segment A4FBh
ī‚ˇ To get the physical address, the segment number is multiplied by 16 (shifted 4 bits to the left) and the offset
is added
ī‚ˇ A4FB0h + 4872h = A9822h (20 bit physical address)
ī‚ˇ There is a lot of overlap between segments; a new segment begins every 16 bytes (addresses ending in 0h)
ī‚ˇ We call these 16 bytes a paragraph
ī‚ˇ Because segments may overlap, the segment:offset address is not unique
8086 Registers
ī‚ˇ Information inside the microprocessor is stored in registers (fourteen 16-bit registers)
ī‚ˇ data registers hold data for an operation
ī‚ˇ address registers hold the address of an instruction or data
ī‚ˇ The address registers are divided into segment, pointer, and index registers
ī‚ˇ a status register (called FLAGS) keeps the current status of the processor
Data Registers: AX, BX, CX, and DX
ī‚ˇ Available to the programmer for general data manipulation
ī‚ˇ Some operations require a particular register
ī‚ˇ High and low bytes of data registers can be accessed separately, i.e., AX is divided into AH and AL
ī‚ˇ AX (accumulator) is preferred for arithmetic, logic, and data transfer operations
ī‚ˇ BX (base register) serves as an address register
ī‚ˇ CX (count register) frequently serves as a loop counter
ī‚ˇ DX (data register) is used in multiplication and division
Pointer and Index Registers: SP, BP, SI, DI
ī‚ˇ SP (stack pointer) points to the top of the processor's stack
ī‚ˇ BP (base pointer) usually accesses data on the stack
ī‚ˇ SI (source index) used to point to memory locations in the data segment
ī‚ˇ DI (destination index) performs same functions as SI.
ī‚ˇ DI and SI are often used for string operations
Segment Registers: CS, DS, SS, ES
ī‚ˇ CS (code segment) addresses the start of the program's machine code in memory
ī‚ˇ DS (data segment) addresses the start of the program's data in memory
ī‚ˇ SS (stack segment) addresses the start of the program's stack space in memory
ī‚ˇ ES (extra segment) addresses and additional data segment, if necessary
Instruction Pointer: IP
ī‚ˇ 8086 uses registers CS and IP to access instructions
ī‚ˇ CS register contains the segment number of the next instruction and the IP contains the offset
ī‚ˇ The IP is updated each time an instruction is executed so it will point to the next instruction
ī‚ˇ The IP is not directly accessible to the user
The FLAGS register
ī‚ˇ Indicates the status of the microprocessor
ī‚ˇ Two kinds of flag bits: status flags and control flags
ī‚ˇ Status flags reflect the result of an instruction, e.g., when the result of an arithmetic operation is 0, ZF (zero
flag) is set to 1 (true)
ī‚ˇ Control flags enable or disable certain operations of the processor, e.g., if the IF (interrupt flag) is cleared
(set to 0), inputs from the keyboard are ignored by the processor
Instructions Groups and Concepts
ī‚ˇ Data Transfer Instructions
ī‚ˇ Arithmetic Instructions
ī‚ˇ Logic Instructions
ī‚ˇ Flow-control Instructions
ī‚ˇ Processor Control Instructions
ī‚ˇ String Instructions
Data Transfer Instructions
ī‚ˇ General instructions
o mov, pop, push, xchg, xlat/xlatb
ī‚ˇ Input/Output instructions
o in, out
ī‚ˇ Address instructions
o lds, lea, les
ī‚ˇ Flag instructions
o lahf, popf, pushf, sahf
General Instructions
ī‚ˇ mov destination, source
ī‚ˇ pop destination
ī‚ˇ push source
ī‚ˇ xchg destination, source
ī‚ˇ xlat(b)table
ī‚ˇ Note that the destination comes first, just as in an assignment statement in C
Examples
ī‚ˇ mov ax, [word1]
o "Move word1 to ax"
o Contents of register ax are replaced by the contents of the memory location word1
o The brackets specify that the contents of word1 are stored -- word1==address,
[word1]==contents
ī‚ˇ xchg ah, bl
o Swaps the contents of ahand bl
ī‚ˇ Illegal: mov [word1], [word2]
o can't have both operands be memory locations
The Stack
ī‚ˇ A data structure in which items are added and removed only from one end (the "top")
ī‚ˇ A program must set aside a block of memory to hold the stack by declaring a stack segment
stack 256
ī‚ˇ SS will contain the segment number of the stack segment -- SP will be initialized to 256 (100h)
ī‚ˇ The stack grows from higher memory addresses to lower ones
PUSH and POP
ī‚ˇ New words are added with push
ī‚ˇ push source
o SP is decreased by 2
o a copy of the source contents is moved to SS:SP
ī‚ˇ Items are removed with pop
ī‚ˇ pop destination
o Content of SS:SP is moved to the destination
o SP is increased by 2
Stack example
push ax ;Save ax and bx
push bx ; on the stack
mov ax, -1 ;Assign test values
mov bx, -2
mov cx, 0
mov dx, 0
push ax ;Push ax onto stack
push bx ;Push bx onto stack
pop cx ;Pop cx from stack
pop dx ;Pop dx from stack
pop bx ;Restore saved ax and bx
pop ax ; values from stack
Arithmetic Instructions
ī‚ˇ Addition instructions
o aaa, adc, add, daa, inc
ī‚ˇ Subtraction instructions
o aas, cmp, das, dec, neg, sbb, sub
ī‚ˇ Multiplication instructions
o aam, imul, mul
ī‚ˇ Division instructions
o aad, cbw, cwd, div, idiv
Addition Instructions
ī‚ˇ aaa
o ASCII adjust for addition
ī‚ˇ adc destination, source
o Add with carry
ī‚ˇ add destination, source
o Add bytes or words
ī‚ˇ daa
o Decimal adjust for addition
ī‚ˇ inc destination
o Increment
ADD and INC
ī‚ˇ ADD is used to add the contents of
o two registers
o a register and a memory location
o a register and a constant
ī‚ˇ INC is used to add 1 to the contents of a register or memory location
Examples
ī‚ˇ add ax, [word1]
o "Add word1to ax"
o Contents of register ax and memory location word1 are added, and the sum is stored in ax
ī‚ˇ inc ah
o Adds one to the contents of ah
ī‚ˇ Illegal: add [word1], [word2]
o can't have both operands be memory locations
Subtraction instructions
ī‚ˇ aas
o ASCII adjust for subtraction
ī‚ˇ cmp destination, source
o Compare
ī‚ˇ das
o Decimal adjust for subtraction
ī‚ˇ dec destination
o Decrement byte or word
ī‚ˇ neg destination
o Negate (two's complement)
ī‚ˇ sbb destination, source
o Subtract with borrow
ī‚ˇ sub destination, source
o Subtract
Examples
ī‚ˇ sub ax, [word1]
o "Subtract word1 from ax"
o Contents of memory location word1 is subtracted from the contents of register ax, and the sum is
stored in ax
ī‚ˇ dec bx
o Subtracts one from the contents of bx
ī‚ˇ Illegal: sub [byte1], [byte2]
o can't have both operands be memory locations
Multiplication instructions
ī‚ˇ aam
o ASCII adjust for multiply
ī‚ˇ imul source
o Integer (signed) multiply
ī‚ˇ mul source
o Unsigned multiply
Byte and Word Multiplication
ī‚ˇ If two bytes are multiplied, the result is a 16-bit word
ī‚ˇ If two words are multiplied, the result is a 32-bit doubleword
ī‚ˇ For the byte form, one number is contained in the source and the other is assumed to be in al -- the product
will be in ax
ī‚ˇ For the word form, one number is contained in the source and the other is assumed to be in ax -- the most
significant 16 bits of the product will be in dx and the least significant 16 bits will be in ax
Examples
ī‚ˇ If ax contains 0002h and bx contains 01FFh
mul bx
dx = 0000h ax = 03FEh
ī‚ˇ If ax contains 0001h and bx contains FFFFh
mul bx
dx = 0000h ax = FFFFh
imul bx
dx = FFFFh ax = FFFFh
Division instructions
ī‚ˇ aad
o ASCII adjust for divide
ī‚ˇ cbw
o convert byte to word
ī‚ˇ cwd
o convert word to doubleword
ī‚ˇ div source
o unsigned divide
ī‚ˇ idiv source
o integer (signed) divide
Byte and Word Division
ī‚ˇ When division is performed, there are two results, the quotient and the remainder
ī‚ˇ These instructions divide 8 (or 16) bits into 16 (or 32) bits
ī‚ˇ Quotient and remainder are same size as the divisor
ī‚ˇ For the byte form, the 8 bit divisor is contained in the source and the dividend is assumed to be in ax -- the
quotient will be in al and the remainder in ah
ī‚ˇ For the word form, the 16 bit divisor is contained in the source and the dividend is assumed to be in dx:ax -
- the quotient will be in ax and the remainder in dx
Examples
ī‚ˇ If dx = 0000h, ax = 00005h, and bx = 0002h
div bx
ax = 0002h dx = 0001h
ī‚ˇ If dx = 0000h, ax = 0005h, and bx = FFFEh
div bx
ax = 0000h dx = 0005h
idiv bx
ax = FFFEh dx = 0001h
Divide Overflow
ī‚ˇ It is possible that the quotient will be too big to fit in the specified destination (al or ax)
ī‚ˇ This can happen if the divisor is much smaller than the dividend
ī‚ˇ When this happens, the program terminates and the system displays the message "Divide Overflow"
Sign Extension of the Dividend
ī‚ˇ Word division
o The dividend is in dx:ax even if the actual dividend will fit in ax
o For div, dx should be cleared
o For idiv, dx should be made the sign extension of ax using cwd
ī‚ˇ Byte division
o The dividend is in ax even if the actual dividend will fit in al
o For div, ah should be cleared
o For idiv, ah should be made the sign extension of al using cbw
Logic Instructions
ī‚ˇ and destination, source
o Logical AND
ī‚ˇ not destination
o Logical NOT (one's complement)
ī‚ˇ or destination, source
o Logical OR
ī‚ˇ test destination, source
o Test bits
ī‚ˇ xor destination, source
o Logical Exclusive OR
ī‚ˇ The ability to manipulate bits is one of the advantages of assembly language
ī‚ˇ One use of and, or, and xor is to selectively modify the bits in the destination using a bit pattern (mask)
ī‚ˇ The and instruction can be used to clear specific destination bits
ī‚ˇ The or instruction can be used to set specific destination bits
ī‚ˇ The xor instruction can be used to complement specific destination bits
Examples
ī‚ˇ To clear the sign bit of al while leaving the other bits unchanged, use the and instruction with 01111111b
=7Fh as the mask
and al,7Fh
ī‚ˇ To set the most significant and least significant bits of al while preserving the other bits, use the or
instruction with 10000001b = 81h as the mask
or al,81h
ī‚ˇ To change the sign bit of dx, use the xor instruction with a mask of 8000h
xor dx,8000h
The NOT instruction
ī‚ˇ The not instruction performs the one's complement operation on the destination
ī‚ˇ The format is
o not destination
ī‚ˇ To complement the bits in ax:
o not ax
ī‚ˇ To complement the bits in WORD1
o not [WORD1]
The TEST instruction
ī‚ˇ The test instruction performs an and operation of the destination with the source but does not change the
destination contents
ī‚ˇ The purpose of the test instruction is to set the status flags (discussed later)
Status Flags
Bit Name Symbol
0 Carry flag cf
2 Parity flag pf
4 Auxiliary carry flag af
6 Zero flag zf
7 Sign flag sf
11 Overflow flag of
The Carry Flag (CF)
ī‚ˇ CF = 1 if there is a carry out from the msb (most significant bit) on addition, or there is a borrow into the
msb on subtraction
ī‚ˇ CF = 0 otherwise
ī‚ˇ CF is also affected by shift and rotate instructions
The Parity Flag (PF)
ī‚ˇ PF = 1 if the low byte of a result has an even number of one bits (even parity)
ī‚ˇ PF = 0 otherwise (odd parity)
The Auxiliary Carry Flag (AF)
ī‚ˇ AF = 1 if there is a carry out from bit 3 on addition, or there is a borrow into the bit 3 on subtraction
ī‚ˇ AF = 0 otherwise
ī‚ˇ AF is used in binary-coded decimal (BCD) operations
The Zero Flag (ZF)
ī‚ˇ ZF = 1 for a zero result
ī‚ˇ ZF = 0 for a non-zero result
The Sign Flag (SF)
ī‚ˇ SF = 1 if the msb of a result is 1; it means the result is negative if you are giving a signed interpretation
ī‚ˇ SF = 0 if the msb is 0
The Overflow Flag (OF)
ī‚ˇ OF = 1 if signed overflow occurred
ī‚ˇ OF = 0 otherwise
Shift Instructions
ī‚ˇ Shift and rotate instructions shift the bits in the destination operand by one or more positions either to the
left or right
ī‚ˇ The instructions have two formats:
o opcode destination, 1
o opcode destination, cl
ī‚ˇ The first shifts by one position, the second shifts by N positions, where cl contains N (cl is the only register
which can be used)
Left Shift Instructions
ī‚ˇ The SHL (shift left) instruction shifts the bits in the destination to the left.
ī‚ˇ Zeros are shifted into the rightmost bit positions and the last bit shifted out goes into CF
ī‚ˇ Effect on flags:
o SF, PF, ZF reflect the result
o AF is undefined
o CF = last bit shifted out
o OF = 1 if result changes sign on last shift
SHL example
ī‚ˇ dh contains 8Ah and cl contains 03h
ī‚ˇ dh = 10001010, cl = 00000011
ī‚ˇ after shl dh,cl
o dh = 01010000, cf = 0
The SAL instruction
ī‚ˇ The shl instruction can be used to multiply an operand by powers of 2
ī‚ˇ To emphasize the arithmetic nature of the operation, the opcode sal (shift arithmetic left) is used in
instances where multiplication is intended
ī‚ˇ Both instructions generate the same machine code
Right Shift Instructions
ī‚ˇ The SHR (shift right) instruction shifts the bits in the destination to the right.
ī‚ˇ Zeros are shifted into the leftmost bit positions and the last bit shifted out goes into CF
ī‚ˇ Effect on flags:
o SF, PF, ZF reflect the result
o AF is undefined
o CF = last bit shifted out
o OF = 1 if result changes on last shift
SHR example
ī‚ˇ dh contains 8Ah and cl contains 02h
ī‚ˇ dh = 10001010, cl = 00000010
ī‚ˇ after shr dh,cl
o dh = 001000010, cf = 1
The SAR instruction
ī‚ˇ The sar (shift arithmetic right) instruction can be used to divide an operand by powers of 2
ī‚ˇ sar operates like shr, except the msb retains its original value
ī‚ˇ The effect on the flags is the same as for shr
ī‚ˇ If unsigned division is desired, shr should be used instead of sar
Rotate Instructions
ī‚ˇ Rotate Left
o The instruction rol (rotate left) shifts bits to the left
o The msb is shifted into the rightmost bit
o The cf also gets the the bit shifted out of the msb
ī‚ˇ Rotate Right
o ror (rotate right) rotates bits to the right
o the rightmost bit is shifted into the msb and also into the cf
Rotate through Carry
ī‚ˇ Rotate through Carry Left
o The instruction rcl shifts bits to the left
o The msb is shifted into cf
o cf is shifted into the rightmost bit
ī‚ˇ Rotate through Carry Right
o rcr rotates bits to the right
o The rightmost bit is shifted into cf
o cf is shifted into the msb
ī‚ˇ See SHIFT.ASM for an example
Flow-Control Instructions
%TITLE "IBM Character Display -- XASCII.ASM"
IDEAL
MODEL small
STACK 256
CODESEG
Start: mov ax, @data ; Initialize DS to address
mov ds, ax ; of data segment
mov ah, 02h ; display character function
mov cx,256 ; no. of chars to display
mov dl, 0 ; dl has ASCII code of null char
Ploop: int 21h ; display a character
inc dl ; increment ASCII code
dec cx ; decrement counter
jnz Ploop ; keep going if cx not zero
Exit: mov ah, 04Ch ; DOS function: Exit program
mov al, 0 ; Return exit code value
int 21h ; Call DOS. Terminate program
END Start ; End of program / entry point
Conditional Jumps
ī‚ˇ jnz is an example of a conditional jump
ī‚ˇ Format is
jxxx destination_label
ī‚ˇ If the condition for the jump is true, the next instruction to be executed is the one at destination_label.
ī‚ˇ If the condition is false, the instruction immediately following the jump is done next
ī‚ˇ For jnz, the condition is that the result of the previous operation is not zero
Range of a Conditional Jump
ī‚ˇ Table 4.6 (and Table 16.4) shows all the conditional jumps
ī‚ˇ The destination_label must precede the jump instruction by no more than 126 bytes, or follow it by no
more than 127 bytes
ī‚ˇ There are ways around this restriction (using the unconditional jmp instruction)
The CMP Instruction
ī‚ˇ The jump condition is often provided by the cmp (compare) instruction:
cmp destination, source
ī‚ˇ cmp is just like sub, except that the destination is not changed -- only the flags are set
ī‚ˇ Suppose ax = 7FFFh and bx = 0001h
cmp ax, bx
jg below
zf = 0 and sf = of = 0, so control transfers to label below
Types of Conditional Jumps
ī‚ˇ Signed Jumps:
o jg/jnle, jge/jnl, jl/jnge, jle/jng
ī‚ˇ Unsigned Jumps:
o ja/jnbe, jae/jnb, jb/jnae, jbe/jna
ī‚ˇ Single-Flag Jumps:
o je/jz, jne/jnz, jc, jnc, jo, jno, js, jns, jp/jpe, jnp/jpo
Signed versus Unsigned Jumps
ī‚ˇ Each of the signed jumps has an analogous unsigned jump (e.g., the signed jump jg and the unsigned jump
ja)
ī‚ˇ Which jump to use depends on the context
ī‚ˇ Using the wrong jump can lead to incorrect results
ī‚ˇ When working with standard ASCII character, either signed or unsigned jumps are OK (msb is always 0)
ī‚ˇ When working with the IBM extended ASCII codes, use unsigned jumps
Conditional Jump Example
ī‚ˇ Suppose ax and bx contained signed numbers. Write some code to put the biggest one in cx:
mov cx,ax ; put ax in cx
cmp bx,cx ; is bx bigger?
jle NEXT ; no, go on
mov cx,bx ; yes, put bx in cx
NEXT:
The JMP Instruction
ī‚ˇ jmp causes an unconditional jump
ī‚ˇ jmp destination
ī‚ˇ jmp can be used to get around the range restriction of a conditional jump
ī‚ˇ e.g, (this example can be made shorter, how?)
TOP: TOP:
; body of loop ; body of loop
; over 126 bytes dec cx
dec cx jnz BOTTOM
jnz TOP jmp EXIT
mov ax, bx BOTTOM:
jmp TOP
EXIT:
mov ax, bx
Branching Structures
ī‚ˇ IF-THEN
ī‚ˇ IF-THEN-ELSE
ī‚ˇ CASE
ī‚ˇ AND conditions
ī‚ˇ OR conditions
IF-THEN structure
ī‚ˇ Example -- to compute |ax|:
if ax < 0 then
ax = -ax
endif
ī‚ˇ Can be coded as:
; if ax < 0
cmp ax, 0 ; ax < 0 ?
jnl endif ; no, exit
; then
neg ax ; yes, change sign
; endif
IF-THEN-ELSE structure
ī‚ˇ Example -- Suppose al and bl contain extended ASCII characters. Display the one that comes first in the
character sequence:
if al <= bl then
display the character in al
else
display the character in bl
endif
ī‚ˇ This example may be coded as:
mov ah, 2 ; prepare for display
; if al <= bl
cmp al, bl ; al <= bl ?
jnbe else ; no, display bl
; then ; al <= bl
mov dl, al ; move it to dl
jmp display
else: ; bl < al
mov dl, bl
display:
int 21h ; display it
; endif
The CASE structure
ī‚ˇ Multi-way branch structure with following form:
case expression
value1 : statement1
value2 : statement2
īŋŊ
valuen : statementn
endcase
ī‚ˇ Example -- If ax contains a negative number, put -1 in bx; if 0, put 0 in bx; if positive, put 1 in bx:
case ax
< 0: put -1 in bx
= 0: put 0 in bx
> 0: put 1 in bx
endcase
ī‚ˇ This example may be coded as:
; case ax
cmp ax, 0 ; test ax
jl neg ; ax < 0
je zero ; ax = 0
jg pos ; ax > 0
neg:
mov bx, -1
jmp endcase
zero:
xor bx,bx ; put 0 in bx
jmp endcase
pos:
mov bx, 0
endcase:
ī‚ˇ Only one cmp is needed, because jump instructions do not affect the flags
AND conditions
ī‚ˇ Example -- read a character and display it if it is uppercase:
read a character into al
if char >= 'A' and char <= 'Z' then
display character
endif
; read a character
mov ah, 1 ;prepare to read
int 21h ;char in al
; if char >= 'A' and char <= 'Z'
cmp al,'A' ;char >= 'A'?
jnge endif ;no, exit
cmp al,'Z' ;char <= 'Z'?
jnle endif ;no, exit
;then display character
mov dl,al ;get char
mov ah,2 ;prep for display
int 21h ;display char
endif:
OR conditions
ī‚ˇ Example -- read a character and display it if it is 'Y' or 'y':
read a character into al
if char = 'y' or char = 'Y' then
display character
endif
; read a character
mov ah, 1 ;prepare to read
int 21h ;char in al
; if char = 'y' or char = 'Y'
cmp al,'y' ;char = 'y'?
je then ;yes, display it
cmp al,'Y' ;char = 'Y'?
je then ;yes, display it
jmp endif ;no, exit
then:
mov ah,2 ;prep for display
mov dl,al ;move char
int 21h ;display char
endif:
Looping Structures
ī‚ˇ FOR loop
ī‚ˇ WHILE loop
ī‚ˇ REPEAT loop
The FOR Loop
ī‚ˇ The loop statements are repeated a known number of times (counter-controlled loop)
for loop_count times do
statements
endfor
ī‚ˇ The loop instruction implements a FOR loop:
loop destination_label
ī‚ˇ The counter for the loop is the register cx which is initialized to loop_count
ī‚ˇ The loop instruction causes cx to be decremented, and if cxš 0, jump to destination_label
ī‚ˇ The destination label must precede the loop instruction by no more than 126 bytes
ī‚ˇ A FOR loop can be implemented as follows:
;initialize cx to loop_count
TOP:
;body of the loop
loop TOP
FOR loop example
ī‚ˇ a count-controlled loop to display a row of 80 stars
mov cx,80 ; # of stars
mov ah,2 ; disp char fnctn
mov dl,'*' ; char to display
TOP:
int 21h ; display a star
loop TOP ; repeat 80 times
FOR loop "gotcha"
ī‚ˇ The FOR loop implemented with the loop instruction always executes at least once
ī‚ˇ If cx = 0 at the beginning, the loop will execute 65536 times!
ī‚ˇ To prevent this, use a jcxz before the loop
jcxz SKIP
TOP:
; body of loop
loop TOP
SKIP:
The WHILE Loop
while condition do
statements
endwhile
ī‚ˇ The condition is checked at the top of the loop
ī‚ˇ The loop executes as long as the condition is true
ī‚ˇ The loop executes 0 or more times
WHILE example
ī‚ˇ Count the number of characters in an input line
count = 0
read char
while char <> carriage_return do
increment count
read char
endwhile
mov dx,0 ;DX counts chars
mov ah,1 ;read char fnctn
int 21h ;read char into al
WHILE_:
cmp al,0Dh ;ASCII CR?
je ENDWHILE ;yes, exit
inc dx ;not CR, inc count
int 21h ;read another char
jmp WHILE_ ;loop back
ENDWHILE:
ī‚ˇ The label WHILE_ is used because WHILE is a reserved word
The REPEAT Loop
repeat
statements
until condition
ī‚ˇ The condition is checked at the bottom of the loop
ī‚ˇ The loop executes until the condition is true
ī‚ˇ The loop executes 1 or more times
REPEAT example
ī‚ˇ read characters until a blank is read
repeat
read character
until character is a blank
mov ah,1 ;read char fnctn
REPEAT:
int 21h ;read char into al
;until
cmp al,' ' ;a blank?
jne REPEAT ;no, keep reading
ī‚ˇ Using a while or a repeat is often a matter of personal preference. The repeat may be a little shorter
because only one jump instruction is required, rather than two
Digression: Displaying a String
ī‚ˇ We've seen INT 21h, functions 1 and 2, to read and display a single character
ī‚ˇ INT 21h, function 9 displays a character string
o Input: dx = offset address of string
o The string must end with a '$' character -- The '$' is not displayed
The LEA Instruction
ī‚ˇ INT 21h, function 9, expects the offset address of the string to be in dx
ī‚ˇ To get it there, use the lea (load effective address) instruction
lea destination,source
ī‚ˇ destination is a register, and source is a memory location
ī‚ˇ For example, lea dx, msg puts the offset address of the variable msg into dx
A digression from our digression -- program segment prefix (PSP)
ī‚ˇ DOS prefaces each program it loads with a PSP
ī‚ˇ The PSP contains information about the program, including any command line arguments
ī‚ˇ The segment number of the PSP is loaded in ds, so ds does not contain the segment number of the
DATASEG
ī‚ˇ To correct this
mov ax,@data
mov ds,ax
ī‚ˇ The assembler translates@data into a segment number
ī‚ˇ Two instructions are necessary since a number cannot be moved directly into a segment register
So, back to printing a string...
%TITLE "Print String Program -- PRTSTR.ASM"
IDEAL
MODEL small
STACK 256
DATASEG
msg DB "Hello!$"
CODESEG
Start:
mov ax,@data ;Initialize DS to address
mov ds,ax ; of data segment
lea dx,[msg] ;get message
mov ah,09h ;display string function
int 21h ;display message
Exit:
mov ah,4Ch ;DOS function: Exit program
mov al,0 ;Return exit code value
int 21h ;Call DOS. Terminate program
END Start ;End of program / entry point
Topic 6 - Programming in Assembly Language_230517_115118.pdf

More Related Content

Similar to Topic 6 - Programming in Assembly Language_230517_115118.pdf

Instruction set of 8086
Instruction set of 8086Instruction set of 8086
Instruction set of 8086
Vijay Kumar
 
Instructionsetof8086 180224060745(3)
Instructionsetof8086 180224060745(3)Instructionsetof8086 180224060745(3)
Instructionsetof8086 180224060745(3)
AmitPaliwal20
 
Lecture5(1)
Lecture5(1)Lecture5(1)
Lecture5(1)
misgina Mengesha
 
Lecture5
Lecture5Lecture5
Lecture5
misgina Mengesha
 
Arithmetic instrctions
Arithmetic instrctionsArithmetic instrctions
Arithmetic instrctions
HarshitParkar6677
 
Wk1to4
Wk1to4Wk1to4
Wk1to4
raymondmy08
 
Introduction of 8086 micro processor .
Introduction of 8086 micro processor .Introduction of 8086 micro processor .
Introduction of 8086 micro processor .
Siraj Ahmed
 
system software 16 marks
system software 16 markssystem software 16 marks
system software 16 marks
vvcetit
 
Instruction sets of 8086
Instruction sets of 8086Instruction sets of 8086
Instruction sets of 8086
Mahalakshmiv11
 
16-bit microprocessors
16-bit microprocessors16-bit microprocessors
16-bit microprocessors
Zahra Sadeghi
 
8086 instruction set (with simulator)
8086 instruction set (with simulator)8086 instruction set (with simulator)
8086 instruction set (with simulator)
Aswini Dharmaraj
 
Assembly Language Basics
Assembly Language BasicsAssembly Language Basics
Assembly Language Basics
Education Front
 
Chapter 3 programming concepts-ii
Chapter 3  programming concepts-iiChapter 3  programming concepts-ii
Chapter 3 programming concepts-ii
SHREEHARI WADAWADAGI
 
address5ng modes.pptx IS A GOOD MATERIAL
address5ng  modes.pptx IS A GOOD MATERIALaddress5ng  modes.pptx IS A GOOD MATERIAL
address5ng modes.pptx IS A GOOD MATERIAL
Drkoteswararaoseelam
 
Chapter 1 archietecture of 8086
Chapter 1 archietecture of 8086Chapter 1 archietecture of 8086
Chapter 1 archietecture of 8086
SHREEHARI WADAWADAGI
 
Intrl 8086 instruction set
Intrl 8086 instruction setIntrl 8086 instruction set
Intrl 8086 instruction set
edwardkiwalabye1
 
Instruction set of 8086
Instruction set of 8086Instruction set of 8086
Instruction set of 8086
Tirumalesh Nizampatnam
 
Instruction Set of 8086 Microprocessor
Instruction Set of 8086 MicroprocessorInstruction Set of 8086 Microprocessor
Instruction Set of 8086 Microprocessor
Ashita Agrawal
 
Chapter1c
Chapter1cChapter1c
8085 instructions
8085 instructions8085 instructions
8085 instructions
Akshay Sharma
 

Similar to Topic 6 - Programming in Assembly Language_230517_115118.pdf (20)

Instruction set of 8086
Instruction set of 8086Instruction set of 8086
Instruction set of 8086
 
Instructionsetof8086 180224060745(3)
Instructionsetof8086 180224060745(3)Instructionsetof8086 180224060745(3)
Instructionsetof8086 180224060745(3)
 
Lecture5(1)
Lecture5(1)Lecture5(1)
Lecture5(1)
 
Lecture5
Lecture5Lecture5
Lecture5
 
Arithmetic instrctions
Arithmetic instrctionsArithmetic instrctions
Arithmetic instrctions
 
Wk1to4
Wk1to4Wk1to4
Wk1to4
 
Introduction of 8086 micro processor .
Introduction of 8086 micro processor .Introduction of 8086 micro processor .
Introduction of 8086 micro processor .
 
system software 16 marks
system software 16 markssystem software 16 marks
system software 16 marks
 
Instruction sets of 8086
Instruction sets of 8086Instruction sets of 8086
Instruction sets of 8086
 
16-bit microprocessors
16-bit microprocessors16-bit microprocessors
16-bit microprocessors
 
8086 instruction set (with simulator)
8086 instruction set (with simulator)8086 instruction set (with simulator)
8086 instruction set (with simulator)
 
Assembly Language Basics
Assembly Language BasicsAssembly Language Basics
Assembly Language Basics
 
Chapter 3 programming concepts-ii
Chapter 3  programming concepts-iiChapter 3  programming concepts-ii
Chapter 3 programming concepts-ii
 
address5ng modes.pptx IS A GOOD MATERIAL
address5ng  modes.pptx IS A GOOD MATERIALaddress5ng  modes.pptx IS A GOOD MATERIAL
address5ng modes.pptx IS A GOOD MATERIAL
 
Chapter 1 archietecture of 8086
Chapter 1 archietecture of 8086Chapter 1 archietecture of 8086
Chapter 1 archietecture of 8086
 
Intrl 8086 instruction set
Intrl 8086 instruction setIntrl 8086 instruction set
Intrl 8086 instruction set
 
Instruction set of 8086
Instruction set of 8086Instruction set of 8086
Instruction set of 8086
 
Instruction Set of 8086 Microprocessor
Instruction Set of 8086 MicroprocessorInstruction Set of 8086 Microprocessor
Instruction Set of 8086 Microprocessor
 
Chapter1c
Chapter1cChapter1c
Chapter1c
 
8085 instructions
8085 instructions8085 instructions
8085 instructions
 

More from ezaldeen2013

Himalaya Cystone for Urinary Tract Infections1.pptx
Himalaya Cystone for Urinary Tract Infections1.pptxHimalaya Cystone for Urinary Tract Infections1.pptx
Himalaya Cystone for Urinary Tract Infections1.pptx
ezaldeen2013
 
اŲ”ØĩŲˆŲ„ اŲ•Ø¯Ø§ØąØŠ.pptx
اŲ”ØĩŲˆŲ„ اŲ•Ø¯Ø§ØąØŠ.pptxاŲ”ØĩŲˆŲ„ اŲ•Ø¯Ø§ØąØŠ.pptx
اŲ”ØĩŲˆŲ„ اŲ•Ø¯Ø§ØąØŠ.pptx
ezaldeen2013
 
Ų†Ų‡Ø§ŲŠŲ”ŲŠ - ØšŲ„Ų… اŲ„اŲ•ØŦØąØ§Ų….pptx
Ų†Ų‡Ø§ŲŠŲ”ŲŠ - ØšŲ„Ų… اŲ„اŲ•ØŦØąØ§Ų….pptxŲ†Ų‡Ø§ŲŠŲ”ŲŠ - ØšŲ„Ų… اŲ„اŲ•ØŦØąØ§Ų….pptx
Ų†Ų‡Ø§ŲŠŲ”ŲŠ - ØšŲ„Ų… اŲ„اŲ•ØŦØąØ§Ų….pptx
ezaldeen2013
 
شبØĒØą 9 ØĒØšŲ‚ŲŠŲ… باŲ„ŲŲ„ØĒØąØŠ - Ų…ؚدŲ„.pptx
شبØĒØą 9 ØĒØšŲ‚ŲŠŲ… باŲ„ŲŲ„ØĒØąØŠ - Ų…ؚدŲ„.pptxشبØĒØą 9 ØĒØšŲ‚ŲŠŲ… باŲ„ŲŲ„ØĒØąØŠ - Ų…ؚدŲ„.pptx
شبØĒØą 9 ØĒØšŲ‚ŲŠŲ… باŲ„ŲŲ„ØĒØąØŠ - Ų…ؚدŲ„.pptx
ezaldeen2013
 
Ų…حاØļØąØŠ 4 - Ø­Øŗاب اŲ„اŲ”Ø­Ų…اŲ„ ŲˆØ§Ų‚ØĒØĩاد اŲ„Ų…ؚداØĒ.pptx
Ų…حاØļØąØŠ 4 - Ø­Øŗاب اŲ„اŲ”Ø­Ų…اŲ„ ŲˆØ§Ų‚ØĒØĩاد اŲ„Ų…ؚداØĒ.pptxŲ…حاØļØąØŠ 4 - Ø­Øŗاب اŲ„اŲ”Ø­Ų…اŲ„ ŲˆØ§Ų‚ØĒØĩاد اŲ„Ų…ؚداØĒ.pptx
Ų…حاØļØąØŠ 4 - Ø­Øŗاب اŲ„اŲ”Ø­Ų…اŲ„ ŲˆØ§Ų‚ØĒØĩاد اŲ„Ų…ؚداØĒ.pptx
ezaldeen2013
 
Ancylostoma.ppt
Ancylostoma.pptAncylostoma.ppt
Ancylostoma.ppt
ezaldeen2013
 
Hyperlipidemia (Hyperlipoproteinaemia).pptx
Hyperlipidemia (Hyperlipoproteinaemia).pptxHyperlipidemia (Hyperlipoproteinaemia).pptx
Hyperlipidemia (Hyperlipoproteinaemia).pptx
ezaldeen2013
 
‎⁨ؚŲ„Ų… اŲ”دŲˆŲŠØŠ 9✅. ⁊.pdf
‎⁨ؚŲ„Ų… اŲ”دŲˆŲŠØŠ 9✅. ⁊.pdf‎⁨ؚŲ„Ų… اŲ”دŲˆŲŠØŠ 9✅. ⁊.pdf
‎⁨ؚŲ„Ų… اŲ”دŲˆŲŠØŠ 9✅. ⁊.pdf
ezaldeen2013
 
‎⁨ؚŲ„Ų… اŲ”دŲˆŲŠØŠ 7✅. ⁊.pdf
‎⁨ؚŲ„Ų… اŲ”دŲˆŲŠØŠ 7✅. ⁊.pdf‎⁨ؚŲ„Ų… اŲ”دŲˆŲŠØŠ 7✅. ⁊.pdf
‎⁨ؚŲ„Ų… اŲ”دŲˆŲŠØŠ 7✅. ⁊.pdf
ezaldeen2013
 
pathogenesis of periodontal diseases_a5d682428760a16d328ef7976e71287b.pdf
pathogenesis of periodontal diseases_a5d682428760a16d328ef7976e71287b.pdfpathogenesis of periodontal diseases_a5d682428760a16d328ef7976e71287b.pdf
pathogenesis of periodontal diseases_a5d682428760a16d328ef7976e71287b.pdf
ezaldeen2013
 
ØĒŲ„ØŽŲŠØĩ اŲ†ŲˆØ§Øš اŲ„ابحاØĢ.pdf
ØĒŲ„ØŽŲŠØĩ اŲ†ŲˆØ§Øš اŲ„ابحاØĢ.pdfØĒŲ„ØŽŲŠØĩ اŲ†ŲˆØ§Øš اŲ„ابحاØĢ.pdf
ØĒŲ„ØŽŲŠØĩ اŲ†ŲˆØ§Øš اŲ„ابحاØĢ.pdf
ezaldeen2013
 
اŲ„ØĒØąŲˆŲŠØŦ_اŲ„دŲˆØ§ØĻŲŠ_ŲƒØĒب_Ųˆ_Ų…ØąØ§ØŦØš_اŲ„Ų‡ŲŠØĻØŠ_اŲ„ØšŲ„ŲŠØ§_Ų„Ų„ØŖدŲˆŲŠØŠ_Ųˆ_اŲ„Ų…ØŗØĒŲ„Ø˛Ų…اØĒ_اŲ„ØˇØ¨ŲŠØŠ.pdf
اŲ„ØĒØąŲˆŲŠØŦ_اŲ„دŲˆØ§ØĻŲŠ_ŲƒØĒب_Ųˆ_Ų…ØąØ§ØŦØš_اŲ„Ų‡ŲŠØĻØŠ_اŲ„ØšŲ„ŲŠØ§_Ų„Ų„ØŖدŲˆŲŠØŠ_Ųˆ_اŲ„Ų…ØŗØĒŲ„Ø˛Ų…اØĒ_اŲ„ØˇØ¨ŲŠØŠ.pdfاŲ„ØĒØąŲˆŲŠØŦ_اŲ„دŲˆØ§ØĻŲŠ_ŲƒØĒب_Ųˆ_Ų…ØąØ§ØŦØš_اŲ„Ų‡ŲŠØĻØŠ_اŲ„ØšŲ„ŲŠØ§_Ų„Ų„ØŖدŲˆŲŠØŠ_Ųˆ_اŲ„Ų…ØŗØĒŲ„Ø˛Ų…اØĒ_اŲ„ØˇØ¨ŲŠØŠ.pdf
اŲ„ØĒØąŲˆŲŠØŦ_اŲ„دŲˆØ§ØĻŲŠ_ŲƒØĒب_Ųˆ_Ų…ØąØ§ØŦØš_اŲ„Ų‡ŲŠØĻØŠ_اŲ„ØšŲ„ŲŠØ§_Ų„Ų„ØŖدŲˆŲŠØŠ_Ųˆ_اŲ„Ų…ØŗØĒŲ„Ø˛Ų…اØĒ_اŲ„ØˇØ¨ŲŠØŠ.pdf
ezaldeen2013
 
7- Quantitative Research- Part 3.pdf
7- Quantitative Research- Part 3.pdf7- Quantitative Research- Part 3.pdf
7- Quantitative Research- Part 3.pdf
ezaldeen2013
 
ØĒŲ„ØŽŲŠØĩ اŲ†ŲˆØ§Øš اŲ„ØšŲŠŲ†Ø§ØĒ ŲˆØĒحدŲŠØ¯ Ų†ŲˆØš اŲ„Ø¯ØąØ§ØŗØŠ ŲˆØ§Ų„Ų…ØĒØēŲŠØąØ§ØĒ Ų…Ų† ØšŲ†ŲˆØ§Ų† اŲ„بحØĢ.pdf
ØĒŲ„ØŽŲŠØĩ اŲ†ŲˆØ§Øš اŲ„ØšŲŠŲ†Ø§ØĒ ŲˆØĒحدŲŠØ¯ Ų†ŲˆØš اŲ„Ø¯ØąØ§ØŗØŠ ŲˆØ§Ų„Ų…ØĒØēŲŠØąØ§ØĒ Ų…Ų† ØšŲ†ŲˆØ§Ų† اŲ„بحØĢ.pdfØĒŲ„ØŽŲŠØĩ اŲ†ŲˆØ§Øš اŲ„ØšŲŠŲ†Ø§ØĒ ŲˆØĒحدŲŠØ¯ Ų†ŲˆØš اŲ„Ø¯ØąØ§ØŗØŠ ŲˆØ§Ų„Ų…ØĒØēŲŠØąØ§ØĒ Ų…Ų† ØšŲ†ŲˆØ§Ų† اŲ„بحØĢ.pdf
ØĒŲ„ØŽŲŠØĩ اŲ†ŲˆØ§Øš اŲ„ØšŲŠŲ†Ø§ØĒ ŲˆØĒحدŲŠØ¯ Ų†ŲˆØš اŲ„Ø¯ØąØ§ØŗØŠ ŲˆØ§Ų„Ų…ØĒØēŲŠØąØ§ØĒ Ų…Ų† ØšŲ†ŲˆØ§Ų† اŲ„بحØĢ.pdf
ezaldeen2013
 
8- Quantitative Research- Part 4.pdf
8- Quantitative Research- Part 4.pdf8- Quantitative Research- Part 4.pdf
8- Quantitative Research- Part 4.pdf
ezaldeen2013
 
ENCH 201 -ch 2 part 2.pdf
ENCH 201 -ch 2 part 2.pdfENCH 201 -ch 2 part 2.pdf
ENCH 201 -ch 2 part 2.pdf
ezaldeen2013
 
ENCH 201 -ch 1.pdf
ENCH 201 -ch 1.pdfENCH 201 -ch 1.pdf
ENCH 201 -ch 1.pdf
ezaldeen2013
 
chap1.pdf
chap1.pdfchap1.pdf
chap1.pdf
ezaldeen2013
 
ENCH 201 -ch 3 part 1.pdf
ENCH 201 -ch 3 part 1.pdfENCH 201 -ch 3 part 1.pdf
ENCH 201 -ch 3 part 1.pdf
ezaldeen2013
 

More from ezaldeen2013 (20)

Himalaya Cystone for Urinary Tract Infections1.pptx
Himalaya Cystone for Urinary Tract Infections1.pptxHimalaya Cystone for Urinary Tract Infections1.pptx
Himalaya Cystone for Urinary Tract Infections1.pptx
 
اŲ”ØĩŲˆŲ„ اŲ•Ø¯Ø§ØąØŠ.pptx
اŲ”ØĩŲˆŲ„ اŲ•Ø¯Ø§ØąØŠ.pptxاŲ”ØĩŲˆŲ„ اŲ•Ø¯Ø§ØąØŠ.pptx
اŲ”ØĩŲˆŲ„ اŲ•Ø¯Ø§ØąØŠ.pptx
 
Ų†Ų‡Ø§ŲŠŲ”ŲŠ - ØšŲ„Ų… اŲ„اŲ•ØŦØąØ§Ų….pptx
Ų†Ų‡Ø§ŲŠŲ”ŲŠ - ØšŲ„Ų… اŲ„اŲ•ØŦØąØ§Ų….pptxŲ†Ų‡Ø§ŲŠŲ”ŲŠ - ØšŲ„Ų… اŲ„اŲ•ØŦØąØ§Ų….pptx
Ų†Ų‡Ø§ŲŠŲ”ŲŠ - ØšŲ„Ų… اŲ„اŲ•ØŦØąØ§Ų….pptx
 
شبØĒØą 9 ØĒØšŲ‚ŲŠŲ… باŲ„ŲŲ„ØĒØąØŠ - Ų…ؚدŲ„.pptx
شبØĒØą 9 ØĒØšŲ‚ŲŠŲ… باŲ„ŲŲ„ØĒØąØŠ - Ų…ؚدŲ„.pptxشبØĒØą 9 ØĒØšŲ‚ŲŠŲ… باŲ„ŲŲ„ØĒØąØŠ - Ų…ؚدŲ„.pptx
شبØĒØą 9 ØĒØšŲ‚ŲŠŲ… باŲ„ŲŲ„ØĒØąØŠ - Ų…ؚدŲ„.pptx
 
Ų…حاØļØąØŠ 4 - Ø­Øŗاب اŲ„اŲ”Ø­Ų…اŲ„ ŲˆØ§Ų‚ØĒØĩاد اŲ„Ų…ؚداØĒ.pptx
Ų…حاØļØąØŠ 4 - Ø­Øŗاب اŲ„اŲ”Ø­Ų…اŲ„ ŲˆØ§Ų‚ØĒØĩاد اŲ„Ų…ؚداØĒ.pptxŲ…حاØļØąØŠ 4 - Ø­Øŗاب اŲ„اŲ”Ø­Ų…اŲ„ ŲˆØ§Ų‚ØĒØĩاد اŲ„Ų…ؚداØĒ.pptx
Ų…حاØļØąØŠ 4 - Ø­Øŗاب اŲ„اŲ”Ø­Ų…اŲ„ ŲˆØ§Ų‚ØĒØĩاد اŲ„Ų…ؚداØĒ.pptx
 
Ancylostoma.ppt
Ancylostoma.pptAncylostoma.ppt
Ancylostoma.ppt
 
Hyperlipidemia (Hyperlipoproteinaemia).pptx
Hyperlipidemia (Hyperlipoproteinaemia).pptxHyperlipidemia (Hyperlipoproteinaemia).pptx
Hyperlipidemia (Hyperlipoproteinaemia).pptx
 
PHV.pptx
PHV.pptxPHV.pptx
PHV.pptx
 
‎⁨ؚŲ„Ų… اŲ”دŲˆŲŠØŠ 9✅. ⁊.pdf
‎⁨ؚŲ„Ų… اŲ”دŲˆŲŠØŠ 9✅. ⁊.pdf‎⁨ؚŲ„Ų… اŲ”دŲˆŲŠØŠ 9✅. ⁊.pdf
‎⁨ؚŲ„Ų… اŲ”دŲˆŲŠØŠ 9✅. ⁊.pdf
 
‎⁨ؚŲ„Ų… اŲ”دŲˆŲŠØŠ 7✅. ⁊.pdf
‎⁨ؚŲ„Ų… اŲ”دŲˆŲŠØŠ 7✅. ⁊.pdf‎⁨ؚŲ„Ų… اŲ”دŲˆŲŠØŠ 7✅. ⁊.pdf
‎⁨ؚŲ„Ų… اŲ”دŲˆŲŠØŠ 7✅. ⁊.pdf
 
pathogenesis of periodontal diseases_a5d682428760a16d328ef7976e71287b.pdf
pathogenesis of periodontal diseases_a5d682428760a16d328ef7976e71287b.pdfpathogenesis of periodontal diseases_a5d682428760a16d328ef7976e71287b.pdf
pathogenesis of periodontal diseases_a5d682428760a16d328ef7976e71287b.pdf
 
ØĒŲ„ØŽŲŠØĩ اŲ†ŲˆØ§Øš اŲ„ابحاØĢ.pdf
ØĒŲ„ØŽŲŠØĩ اŲ†ŲˆØ§Øš اŲ„ابحاØĢ.pdfØĒŲ„ØŽŲŠØĩ اŲ†ŲˆØ§Øš اŲ„ابحاØĢ.pdf
ØĒŲ„ØŽŲŠØĩ اŲ†ŲˆØ§Øš اŲ„ابحاØĢ.pdf
 
اŲ„ØĒØąŲˆŲŠØŦ_اŲ„دŲˆØ§ØĻŲŠ_ŲƒØĒب_Ųˆ_Ų…ØąØ§ØŦØš_اŲ„Ų‡ŲŠØĻØŠ_اŲ„ØšŲ„ŲŠØ§_Ų„Ų„ØŖدŲˆŲŠØŠ_Ųˆ_اŲ„Ų…ØŗØĒŲ„Ø˛Ų…اØĒ_اŲ„ØˇØ¨ŲŠØŠ.pdf
اŲ„ØĒØąŲˆŲŠØŦ_اŲ„دŲˆØ§ØĻŲŠ_ŲƒØĒب_Ųˆ_Ų…ØąØ§ØŦØš_اŲ„Ų‡ŲŠØĻØŠ_اŲ„ØšŲ„ŲŠØ§_Ų„Ų„ØŖدŲˆŲŠØŠ_Ųˆ_اŲ„Ų…ØŗØĒŲ„Ø˛Ų…اØĒ_اŲ„ØˇØ¨ŲŠØŠ.pdfاŲ„ØĒØąŲˆŲŠØŦ_اŲ„دŲˆØ§ØĻŲŠ_ŲƒØĒب_Ųˆ_Ų…ØąØ§ØŦØš_اŲ„Ų‡ŲŠØĻØŠ_اŲ„ØšŲ„ŲŠØ§_Ų„Ų„ØŖدŲˆŲŠØŠ_Ųˆ_اŲ„Ų…ØŗØĒŲ„Ø˛Ų…اØĒ_اŲ„ØˇØ¨ŲŠØŠ.pdf
اŲ„ØĒØąŲˆŲŠØŦ_اŲ„دŲˆØ§ØĻŲŠ_ŲƒØĒب_Ųˆ_Ų…ØąØ§ØŦØš_اŲ„Ų‡ŲŠØĻØŠ_اŲ„ØšŲ„ŲŠØ§_Ų„Ų„ØŖدŲˆŲŠØŠ_Ųˆ_اŲ„Ų…ØŗØĒŲ„Ø˛Ų…اØĒ_اŲ„ØˇØ¨ŲŠØŠ.pdf
 
7- Quantitative Research- Part 3.pdf
7- Quantitative Research- Part 3.pdf7- Quantitative Research- Part 3.pdf
7- Quantitative Research- Part 3.pdf
 
ØĒŲ„ØŽŲŠØĩ اŲ†ŲˆØ§Øš اŲ„ØšŲŠŲ†Ø§ØĒ ŲˆØĒحدŲŠØ¯ Ų†ŲˆØš اŲ„Ø¯ØąØ§ØŗØŠ ŲˆØ§Ų„Ų…ØĒØēŲŠØąØ§ØĒ Ų…Ų† ØšŲ†ŲˆØ§Ų† اŲ„بحØĢ.pdf
ØĒŲ„ØŽŲŠØĩ اŲ†ŲˆØ§Øš اŲ„ØšŲŠŲ†Ø§ØĒ ŲˆØĒحدŲŠØ¯ Ų†ŲˆØš اŲ„Ø¯ØąØ§ØŗØŠ ŲˆØ§Ų„Ų…ØĒØēŲŠØąØ§ØĒ Ų…Ų† ØšŲ†ŲˆØ§Ų† اŲ„بحØĢ.pdfØĒŲ„ØŽŲŠØĩ اŲ†ŲˆØ§Øš اŲ„ØšŲŠŲ†Ø§ØĒ ŲˆØĒحدŲŠØ¯ Ų†ŲˆØš اŲ„Ø¯ØąØ§ØŗØŠ ŲˆØ§Ų„Ų…ØĒØēŲŠØąØ§ØĒ Ų…Ų† ØšŲ†ŲˆØ§Ų† اŲ„بحØĢ.pdf
ØĒŲ„ØŽŲŠØĩ اŲ†ŲˆØ§Øš اŲ„ØšŲŠŲ†Ø§ØĒ ŲˆØĒحدŲŠØ¯ Ų†ŲˆØš اŲ„Ø¯ØąØ§ØŗØŠ ŲˆØ§Ų„Ų…ØĒØēŲŠØąØ§ØĒ Ų…Ų† ØšŲ†ŲˆØ§Ų† اŲ„بحØĢ.pdf
 
8- Quantitative Research- Part 4.pdf
8- Quantitative Research- Part 4.pdf8- Quantitative Research- Part 4.pdf
8- Quantitative Research- Part 4.pdf
 
ENCH 201 -ch 2 part 2.pdf
ENCH 201 -ch 2 part 2.pdfENCH 201 -ch 2 part 2.pdf
ENCH 201 -ch 2 part 2.pdf
 
ENCH 201 -ch 1.pdf
ENCH 201 -ch 1.pdfENCH 201 -ch 1.pdf
ENCH 201 -ch 1.pdf
 
chap1.pdf
chap1.pdfchap1.pdf
chap1.pdf
 
ENCH 201 -ch 3 part 1.pdf
ENCH 201 -ch 3 part 1.pdfENCH 201 -ch 3 part 1.pdf
ENCH 201 -ch 3 part 1.pdf
 

Recently uploaded

Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
Zilliz
 
Operating System Used by Users in day-to-day life.pptx
Operating System Used by Users in day-to-day life.pptxOperating System Used by Users in day-to-day life.pptx
Operating System Used by Users in day-to-day life.pptx
Pravash Chandra Das
 
dbms calicut university B. sc Cs 4th sem.pdf
dbms  calicut university B. sc Cs 4th sem.pdfdbms  calicut university B. sc Cs 4th sem.pdf
dbms calicut university B. sc Cs 4th sem.pdf
Shinana2
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
panagenda
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
Brandon Minnick, MBA
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
Zilliz
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
Ivanti
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
Zilliz
 
Azure API Management to expose backend services securely
Azure API Management to expose backend services securelyAzure API Management to expose backend services securely
Azure API Management to expose backend services securely
Dinusha Kumarasiri
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
Tatiana Kojar
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
Chart Kalyan
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
tolgahangng
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
saastr
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
saastr
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
MichaelKnudsen27
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Alpen-Adria-Universität
 
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
Jeffrey Haguewood
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
Jason Packer
 
Nunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdf
Nunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdfNunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdf
Nunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdf
flufftailshop
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
Zilliz
 

Recently uploaded (20)

Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
 
Operating System Used by Users in day-to-day life.pptx
Operating System Used by Users in day-to-day life.pptxOperating System Used by Users in day-to-day life.pptx
Operating System Used by Users in day-to-day life.pptx
 
dbms calicut university B. sc Cs 4th sem.pdf
dbms  calicut university B. sc Cs 4th sem.pdfdbms  calicut university B. sc Cs 4th sem.pdf
dbms calicut university B. sc Cs 4th sem.pdf
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
 
Azure API Management to expose backend services securely
Azure API Management to expose backend services securelyAzure API Management to expose backend services securely
Azure API Management to expose backend services securely
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
 
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
 
Nunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdf
Nunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdfNunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdf
Nunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdf
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
 

Topic 6 - Programming in Assembly Language_230517_115118.pdf

  • 1. Programming in Assembly Language Memory Segmentation ī‚ˇ Memory segments are a direct consequence of using a 20 bit address in a 16 bit processor ī‚ˇ Memory is partitioned into 64K (216 ) segments ī‚ˇ Each segment is identified by a 16-bit segment number ranging from 0000h-FFFFh ī‚ˇ Within a segment, a memory location is specified by a 16-bit offset (the number of bytes from the beginning of the segment) ī‚ˇ The Segment:Offset address is a logical address Segment:Offset Addresses ī‚ˇ A4FB:4872h means offset 4872h within segment A4FBh ī‚ˇ To get the physical address, the segment number is multiplied by 16 (shifted 4 bits to the left) and the offset is added ī‚ˇ A4FB0h + 4872h = A9822h (20 bit physical address) ī‚ˇ There is a lot of overlap between segments; a new segment begins every 16 bytes (addresses ending in 0h) ī‚ˇ We call these 16 bytes a paragraph ī‚ˇ Because segments may overlap, the segment:offset address is not unique 8086 Registers ī‚ˇ Information inside the microprocessor is stored in registers (fourteen 16-bit registers) ī‚ˇ data registers hold data for an operation ī‚ˇ address registers hold the address of an instruction or data ī‚ˇ The address registers are divided into segment, pointer, and index registers ī‚ˇ a status register (called FLAGS) keeps the current status of the processor Data Registers: AX, BX, CX, and DX ī‚ˇ Available to the programmer for general data manipulation ī‚ˇ Some operations require a particular register ī‚ˇ High and low bytes of data registers can be accessed separately, i.e., AX is divided into AH and AL ī‚ˇ AX (accumulator) is preferred for arithmetic, logic, and data transfer operations ī‚ˇ BX (base register) serves as an address register ī‚ˇ CX (count register) frequently serves as a loop counter ī‚ˇ DX (data register) is used in multiplication and division Pointer and Index Registers: SP, BP, SI, DI ī‚ˇ SP (stack pointer) points to the top of the processor's stack ī‚ˇ BP (base pointer) usually accesses data on the stack ī‚ˇ SI (source index) used to point to memory locations in the data segment ī‚ˇ DI (destination index) performs same functions as SI. ī‚ˇ DI and SI are often used for string operations Segment Registers: CS, DS, SS, ES ī‚ˇ CS (code segment) addresses the start of the program's machine code in memory ī‚ˇ DS (data segment) addresses the start of the program's data in memory
  • 2. ī‚ˇ SS (stack segment) addresses the start of the program's stack space in memory ī‚ˇ ES (extra segment) addresses and additional data segment, if necessary Instruction Pointer: IP ī‚ˇ 8086 uses registers CS and IP to access instructions ī‚ˇ CS register contains the segment number of the next instruction and the IP contains the offset ī‚ˇ The IP is updated each time an instruction is executed so it will point to the next instruction ī‚ˇ The IP is not directly accessible to the user The FLAGS register ī‚ˇ Indicates the status of the microprocessor ī‚ˇ Two kinds of flag bits: status flags and control flags ī‚ˇ Status flags reflect the result of an instruction, e.g., when the result of an arithmetic operation is 0, ZF (zero flag) is set to 1 (true) ī‚ˇ Control flags enable or disable certain operations of the processor, e.g., if the IF (interrupt flag) is cleared (set to 0), inputs from the keyboard are ignored by the processor Instructions Groups and Concepts ī‚ˇ Data Transfer Instructions ī‚ˇ Arithmetic Instructions ī‚ˇ Logic Instructions ī‚ˇ Flow-control Instructions ī‚ˇ Processor Control Instructions ī‚ˇ String Instructions Data Transfer Instructions ī‚ˇ General instructions o mov, pop, push, xchg, xlat/xlatb ī‚ˇ Input/Output instructions o in, out ī‚ˇ Address instructions o lds, lea, les ī‚ˇ Flag instructions o lahf, popf, pushf, sahf General Instructions ī‚ˇ mov destination, source ī‚ˇ pop destination ī‚ˇ push source ī‚ˇ xchg destination, source ī‚ˇ xlat(b)table ī‚ˇ Note that the destination comes first, just as in an assignment statement in C Examples
  • 3. ī‚ˇ mov ax, [word1] o "Move word1 to ax" o Contents of register ax are replaced by the contents of the memory location word1 o The brackets specify that the contents of word1 are stored -- word1==address, [word1]==contents ī‚ˇ xchg ah, bl o Swaps the contents of ahand bl ī‚ˇ Illegal: mov [word1], [word2] o can't have both operands be memory locations The Stack ī‚ˇ A data structure in which items are added and removed only from one end (the "top") ī‚ˇ A program must set aside a block of memory to hold the stack by declaring a stack segment stack 256 ī‚ˇ SS will contain the segment number of the stack segment -- SP will be initialized to 256 (100h) ī‚ˇ The stack grows from higher memory addresses to lower ones PUSH and POP ī‚ˇ New words are added with push ī‚ˇ push source o SP is decreased by 2 o a copy of the source contents is moved to SS:SP ī‚ˇ Items are removed with pop ī‚ˇ pop destination o Content of SS:SP is moved to the destination o SP is increased by 2 Stack example push ax ;Save ax and bx push bx ; on the stack mov ax, -1 ;Assign test values mov bx, -2 mov cx, 0 mov dx, 0 push ax ;Push ax onto stack push bx ;Push bx onto stack pop cx ;Pop cx from stack pop dx ;Pop dx from stack pop bx ;Restore saved ax and bx pop ax ; values from stack Arithmetic Instructions ī‚ˇ Addition instructions o aaa, adc, add, daa, inc ī‚ˇ Subtraction instructions o aas, cmp, das, dec, neg, sbb, sub ī‚ˇ Multiplication instructions o aam, imul, mul
  • 4. ī‚ˇ Division instructions o aad, cbw, cwd, div, idiv Addition Instructions ī‚ˇ aaa o ASCII adjust for addition ī‚ˇ adc destination, source o Add with carry ī‚ˇ add destination, source o Add bytes or words ī‚ˇ daa o Decimal adjust for addition ī‚ˇ inc destination o Increment ADD and INC ī‚ˇ ADD is used to add the contents of o two registers o a register and a memory location o a register and a constant ī‚ˇ INC is used to add 1 to the contents of a register or memory location Examples ī‚ˇ add ax, [word1] o "Add word1to ax" o Contents of register ax and memory location word1 are added, and the sum is stored in ax ī‚ˇ inc ah o Adds one to the contents of ah ī‚ˇ Illegal: add [word1], [word2] o can't have both operands be memory locations Subtraction instructions ī‚ˇ aas o ASCII adjust for subtraction ī‚ˇ cmp destination, source o Compare ī‚ˇ das o Decimal adjust for subtraction ī‚ˇ dec destination o Decrement byte or word ī‚ˇ neg destination o Negate (two's complement) ī‚ˇ sbb destination, source o Subtract with borrow ī‚ˇ sub destination, source o Subtract Examples
  • 5. ī‚ˇ sub ax, [word1] o "Subtract word1 from ax" o Contents of memory location word1 is subtracted from the contents of register ax, and the sum is stored in ax ī‚ˇ dec bx o Subtracts one from the contents of bx ī‚ˇ Illegal: sub [byte1], [byte2] o can't have both operands be memory locations Multiplication instructions ī‚ˇ aam o ASCII adjust for multiply ī‚ˇ imul source o Integer (signed) multiply ī‚ˇ mul source o Unsigned multiply Byte and Word Multiplication ī‚ˇ If two bytes are multiplied, the result is a 16-bit word ī‚ˇ If two words are multiplied, the result is a 32-bit doubleword ī‚ˇ For the byte form, one number is contained in the source and the other is assumed to be in al -- the product will be in ax ī‚ˇ For the word form, one number is contained in the source and the other is assumed to be in ax -- the most significant 16 bits of the product will be in dx and the least significant 16 bits will be in ax Examples ī‚ˇ If ax contains 0002h and bx contains 01FFh mul bx dx = 0000h ax = 03FEh ī‚ˇ If ax contains 0001h and bx contains FFFFh mul bx dx = 0000h ax = FFFFh imul bx dx = FFFFh ax = FFFFh Division instructions ī‚ˇ aad o ASCII adjust for divide ī‚ˇ cbw o convert byte to word ī‚ˇ cwd o convert word to doubleword ī‚ˇ div source o unsigned divide ī‚ˇ idiv source o integer (signed) divide
  • 6. Byte and Word Division ī‚ˇ When division is performed, there are two results, the quotient and the remainder ī‚ˇ These instructions divide 8 (or 16) bits into 16 (or 32) bits ī‚ˇ Quotient and remainder are same size as the divisor ī‚ˇ For the byte form, the 8 bit divisor is contained in the source and the dividend is assumed to be in ax -- the quotient will be in al and the remainder in ah ī‚ˇ For the word form, the 16 bit divisor is contained in the source and the dividend is assumed to be in dx:ax - - the quotient will be in ax and the remainder in dx Examples ī‚ˇ If dx = 0000h, ax = 00005h, and bx = 0002h div bx ax = 0002h dx = 0001h ī‚ˇ If dx = 0000h, ax = 0005h, and bx = FFFEh div bx ax = 0000h dx = 0005h idiv bx ax = FFFEh dx = 0001h Divide Overflow ī‚ˇ It is possible that the quotient will be too big to fit in the specified destination (al or ax) ī‚ˇ This can happen if the divisor is much smaller than the dividend ī‚ˇ When this happens, the program terminates and the system displays the message "Divide Overflow" Sign Extension of the Dividend ī‚ˇ Word division o The dividend is in dx:ax even if the actual dividend will fit in ax o For div, dx should be cleared o For idiv, dx should be made the sign extension of ax using cwd ī‚ˇ Byte division o The dividend is in ax even if the actual dividend will fit in al o For div, ah should be cleared o For idiv, ah should be made the sign extension of al using cbw Logic Instructions ī‚ˇ and destination, source o Logical AND ī‚ˇ not destination o Logical NOT (one's complement) ī‚ˇ or destination, source o Logical OR ī‚ˇ test destination, source o Test bits ī‚ˇ xor destination, source o Logical Exclusive OR
  • 7. ī‚ˇ The ability to manipulate bits is one of the advantages of assembly language ī‚ˇ One use of and, or, and xor is to selectively modify the bits in the destination using a bit pattern (mask) ī‚ˇ The and instruction can be used to clear specific destination bits ī‚ˇ The or instruction can be used to set specific destination bits ī‚ˇ The xor instruction can be used to complement specific destination bits Examples ī‚ˇ To clear the sign bit of al while leaving the other bits unchanged, use the and instruction with 01111111b =7Fh as the mask and al,7Fh ī‚ˇ To set the most significant and least significant bits of al while preserving the other bits, use the or instruction with 10000001b = 81h as the mask or al,81h ī‚ˇ To change the sign bit of dx, use the xor instruction with a mask of 8000h xor dx,8000h The NOT instruction ī‚ˇ The not instruction performs the one's complement operation on the destination ī‚ˇ The format is o not destination ī‚ˇ To complement the bits in ax: o not ax ī‚ˇ To complement the bits in WORD1 o not [WORD1] The TEST instruction ī‚ˇ The test instruction performs an and operation of the destination with the source but does not change the destination contents ī‚ˇ The purpose of the test instruction is to set the status flags (discussed later) Status Flags Bit Name Symbol 0 Carry flag cf 2 Parity flag pf 4 Auxiliary carry flag af 6 Zero flag zf 7 Sign flag sf 11 Overflow flag of The Carry Flag (CF) ī‚ˇ CF = 1 if there is a carry out from the msb (most significant bit) on addition, or there is a borrow into the msb on subtraction ī‚ˇ CF = 0 otherwise
  • 8. ī‚ˇ CF is also affected by shift and rotate instructions The Parity Flag (PF) ī‚ˇ PF = 1 if the low byte of a result has an even number of one bits (even parity) ī‚ˇ PF = 0 otherwise (odd parity) The Auxiliary Carry Flag (AF) ī‚ˇ AF = 1 if there is a carry out from bit 3 on addition, or there is a borrow into the bit 3 on subtraction ī‚ˇ AF = 0 otherwise ī‚ˇ AF is used in binary-coded decimal (BCD) operations The Zero Flag (ZF) ī‚ˇ ZF = 1 for a zero result ī‚ˇ ZF = 0 for a non-zero result The Sign Flag (SF) ī‚ˇ SF = 1 if the msb of a result is 1; it means the result is negative if you are giving a signed interpretation ī‚ˇ SF = 0 if the msb is 0 The Overflow Flag (OF) ī‚ˇ OF = 1 if signed overflow occurred ī‚ˇ OF = 0 otherwise Shift Instructions ī‚ˇ Shift and rotate instructions shift the bits in the destination operand by one or more positions either to the left or right ī‚ˇ The instructions have two formats: o opcode destination, 1 o opcode destination, cl ī‚ˇ The first shifts by one position, the second shifts by N positions, where cl contains N (cl is the only register which can be used) Left Shift Instructions ī‚ˇ The SHL (shift left) instruction shifts the bits in the destination to the left. ī‚ˇ Zeros are shifted into the rightmost bit positions and the last bit shifted out goes into CF ī‚ˇ Effect on flags: o SF, PF, ZF reflect the result o AF is undefined o CF = last bit shifted out o OF = 1 if result changes sign on last shift SHL example ī‚ˇ dh contains 8Ah and cl contains 03h
  • 9. ī‚ˇ dh = 10001010, cl = 00000011 ī‚ˇ after shl dh,cl o dh = 01010000, cf = 0 The SAL instruction ī‚ˇ The shl instruction can be used to multiply an operand by powers of 2 ī‚ˇ To emphasize the arithmetic nature of the operation, the opcode sal (shift arithmetic left) is used in instances where multiplication is intended ī‚ˇ Both instructions generate the same machine code Right Shift Instructions ī‚ˇ The SHR (shift right) instruction shifts the bits in the destination to the right. ī‚ˇ Zeros are shifted into the leftmost bit positions and the last bit shifted out goes into CF ī‚ˇ Effect on flags: o SF, PF, ZF reflect the result o AF is undefined o CF = last bit shifted out o OF = 1 if result changes on last shift SHR example ī‚ˇ dh contains 8Ah and cl contains 02h ī‚ˇ dh = 10001010, cl = 00000010 ī‚ˇ after shr dh,cl o dh = 001000010, cf = 1 The SAR instruction ī‚ˇ The sar (shift arithmetic right) instruction can be used to divide an operand by powers of 2 ī‚ˇ sar operates like shr, except the msb retains its original value ī‚ˇ The effect on the flags is the same as for shr ī‚ˇ If unsigned division is desired, shr should be used instead of sar Rotate Instructions ī‚ˇ Rotate Left o The instruction rol (rotate left) shifts bits to the left o The msb is shifted into the rightmost bit o The cf also gets the the bit shifted out of the msb ī‚ˇ Rotate Right o ror (rotate right) rotates bits to the right o the rightmost bit is shifted into the msb and also into the cf Rotate through Carry ī‚ˇ Rotate through Carry Left o The instruction rcl shifts bits to the left o The msb is shifted into cf o cf is shifted into the rightmost bit ī‚ˇ Rotate through Carry Right
  • 10. o rcr rotates bits to the right o The rightmost bit is shifted into cf o cf is shifted into the msb ī‚ˇ See SHIFT.ASM for an example Flow-Control Instructions %TITLE "IBM Character Display -- XASCII.ASM" IDEAL MODEL small STACK 256 CODESEG Start: mov ax, @data ; Initialize DS to address mov ds, ax ; of data segment mov ah, 02h ; display character function mov cx,256 ; no. of chars to display mov dl, 0 ; dl has ASCII code of null char Ploop: int 21h ; display a character inc dl ; increment ASCII code dec cx ; decrement counter jnz Ploop ; keep going if cx not zero Exit: mov ah, 04Ch ; DOS function: Exit program mov al, 0 ; Return exit code value int 21h ; Call DOS. Terminate program END Start ; End of program / entry point Conditional Jumps ī‚ˇ jnz is an example of a conditional jump ī‚ˇ Format is jxxx destination_label ī‚ˇ If the condition for the jump is true, the next instruction to be executed is the one at destination_label. ī‚ˇ If the condition is false, the instruction immediately following the jump is done next ī‚ˇ For jnz, the condition is that the result of the previous operation is not zero Range of a Conditional Jump ī‚ˇ Table 4.6 (and Table 16.4) shows all the conditional jumps ī‚ˇ The destination_label must precede the jump instruction by no more than 126 bytes, or follow it by no more than 127 bytes ī‚ˇ There are ways around this restriction (using the unconditional jmp instruction) The CMP Instruction ī‚ˇ The jump condition is often provided by the cmp (compare) instruction: cmp destination, source ī‚ˇ cmp is just like sub, except that the destination is not changed -- only the flags are set ī‚ˇ Suppose ax = 7FFFh and bx = 0001h cmp ax, bx jg below
  • 11. zf = 0 and sf = of = 0, so control transfers to label below Types of Conditional Jumps ī‚ˇ Signed Jumps: o jg/jnle, jge/jnl, jl/jnge, jle/jng ī‚ˇ Unsigned Jumps: o ja/jnbe, jae/jnb, jb/jnae, jbe/jna ī‚ˇ Single-Flag Jumps: o je/jz, jne/jnz, jc, jnc, jo, jno, js, jns, jp/jpe, jnp/jpo Signed versus Unsigned Jumps ī‚ˇ Each of the signed jumps has an analogous unsigned jump (e.g., the signed jump jg and the unsigned jump ja) ī‚ˇ Which jump to use depends on the context ī‚ˇ Using the wrong jump can lead to incorrect results ī‚ˇ When working with standard ASCII character, either signed or unsigned jumps are OK (msb is always 0) ī‚ˇ When working with the IBM extended ASCII codes, use unsigned jumps Conditional Jump Example ī‚ˇ Suppose ax and bx contained signed numbers. Write some code to put the biggest one in cx: mov cx,ax ; put ax in cx cmp bx,cx ; is bx bigger? jle NEXT ; no, go on mov cx,bx ; yes, put bx in cx NEXT: The JMP Instruction ī‚ˇ jmp causes an unconditional jump ī‚ˇ jmp destination ī‚ˇ jmp can be used to get around the range restriction of a conditional jump ī‚ˇ e.g, (this example can be made shorter, how?) TOP: TOP: ; body of loop ; body of loop ; over 126 bytes dec cx dec cx jnz BOTTOM jnz TOP jmp EXIT mov ax, bx BOTTOM: jmp TOP EXIT: mov ax, bx Branching Structures ī‚ˇ IF-THEN ī‚ˇ IF-THEN-ELSE ī‚ˇ CASE ī‚ˇ AND conditions ī‚ˇ OR conditions IF-THEN structure
  • 12. ī‚ˇ Example -- to compute |ax|: if ax < 0 then ax = -ax endif ī‚ˇ Can be coded as: ; if ax < 0 cmp ax, 0 ; ax < 0 ? jnl endif ; no, exit ; then neg ax ; yes, change sign ; endif IF-THEN-ELSE structure ī‚ˇ Example -- Suppose al and bl contain extended ASCII characters. Display the one that comes first in the character sequence: if al <= bl then display the character in al else display the character in bl endif ī‚ˇ This example may be coded as: mov ah, 2 ; prepare for display ; if al <= bl cmp al, bl ; al <= bl ? jnbe else ; no, display bl ; then ; al <= bl mov dl, al ; move it to dl jmp display else: ; bl < al mov dl, bl display: int 21h ; display it ; endif The CASE structure ī‚ˇ Multi-way branch structure with following form: case expression value1 : statement1 value2 : statement2 īŋŊ valuen : statementn endcase ī‚ˇ Example -- If ax contains a negative number, put -1 in bx; if 0, put 0 in bx; if positive, put 1 in bx:
  • 13. case ax < 0: put -1 in bx = 0: put 0 in bx > 0: put 1 in bx endcase ī‚ˇ This example may be coded as: ; case ax cmp ax, 0 ; test ax jl neg ; ax < 0 je zero ; ax = 0 jg pos ; ax > 0 neg: mov bx, -1 jmp endcase zero: xor bx,bx ; put 0 in bx jmp endcase pos: mov bx, 0 endcase: ī‚ˇ Only one cmp is needed, because jump instructions do not affect the flags AND conditions ī‚ˇ Example -- read a character and display it if it is uppercase: read a character into al if char >= 'A' and char <= 'Z' then display character endif ; read a character mov ah, 1 ;prepare to read int 21h ;char in al ; if char >= 'A' and char <= 'Z' cmp al,'A' ;char >= 'A'? jnge endif ;no, exit cmp al,'Z' ;char <= 'Z'? jnle endif ;no, exit ;then display character mov dl,al ;get char mov ah,2 ;prep for display int 21h ;display char endif: OR conditions ī‚ˇ Example -- read a character and display it if it is 'Y' or 'y': read a character into al if char = 'y' or char = 'Y' then display character endif
  • 14. ; read a character mov ah, 1 ;prepare to read int 21h ;char in al ; if char = 'y' or char = 'Y' cmp al,'y' ;char = 'y'? je then ;yes, display it cmp al,'Y' ;char = 'Y'? je then ;yes, display it jmp endif ;no, exit then: mov ah,2 ;prep for display mov dl,al ;move char int 21h ;display char endif: Looping Structures ī‚ˇ FOR loop ī‚ˇ WHILE loop ī‚ˇ REPEAT loop The FOR Loop ī‚ˇ The loop statements are repeated a known number of times (counter-controlled loop) for loop_count times do statements endfor ī‚ˇ The loop instruction implements a FOR loop: loop destination_label ī‚ˇ The counter for the loop is the register cx which is initialized to loop_count ī‚ˇ The loop instruction causes cx to be decremented, and if cxš 0, jump to destination_label ī‚ˇ The destination label must precede the loop instruction by no more than 126 bytes ī‚ˇ A FOR loop can be implemented as follows: ;initialize cx to loop_count TOP: ;body of the loop loop TOP FOR loop example ī‚ˇ a count-controlled loop to display a row of 80 stars mov cx,80 ; # of stars mov ah,2 ; disp char fnctn mov dl,'*' ; char to display TOP: int 21h ; display a star loop TOP ; repeat 80 times FOR loop "gotcha"
  • 15. ī‚ˇ The FOR loop implemented with the loop instruction always executes at least once ī‚ˇ If cx = 0 at the beginning, the loop will execute 65536 times! ī‚ˇ To prevent this, use a jcxz before the loop jcxz SKIP TOP: ; body of loop loop TOP SKIP: The WHILE Loop while condition do statements endwhile ī‚ˇ The condition is checked at the top of the loop ī‚ˇ The loop executes as long as the condition is true ī‚ˇ The loop executes 0 or more times WHILE example ī‚ˇ Count the number of characters in an input line count = 0 read char while char <> carriage_return do increment count read char endwhile mov dx,0 ;DX counts chars mov ah,1 ;read char fnctn int 21h ;read char into al WHILE_: cmp al,0Dh ;ASCII CR? je ENDWHILE ;yes, exit inc dx ;not CR, inc count int 21h ;read another char jmp WHILE_ ;loop back ENDWHILE: ī‚ˇ The label WHILE_ is used because WHILE is a reserved word The REPEAT Loop repeat statements until condition ī‚ˇ The condition is checked at the bottom of the loop ī‚ˇ The loop executes until the condition is true ī‚ˇ The loop executes 1 or more times REPEAT example ī‚ˇ read characters until a blank is read
  • 16. repeat read character until character is a blank mov ah,1 ;read char fnctn REPEAT: int 21h ;read char into al ;until cmp al,' ' ;a blank? jne REPEAT ;no, keep reading ī‚ˇ Using a while or a repeat is often a matter of personal preference. The repeat may be a little shorter because only one jump instruction is required, rather than two Digression: Displaying a String ī‚ˇ We've seen INT 21h, functions 1 and 2, to read and display a single character ī‚ˇ INT 21h, function 9 displays a character string o Input: dx = offset address of string o The string must end with a '$' character -- The '$' is not displayed The LEA Instruction ī‚ˇ INT 21h, function 9, expects the offset address of the string to be in dx ī‚ˇ To get it there, use the lea (load effective address) instruction lea destination,source ī‚ˇ destination is a register, and source is a memory location ī‚ˇ For example, lea dx, msg puts the offset address of the variable msg into dx A digression from our digression -- program segment prefix (PSP) ī‚ˇ DOS prefaces each program it loads with a PSP ī‚ˇ The PSP contains information about the program, including any command line arguments ī‚ˇ The segment number of the PSP is loaded in ds, so ds does not contain the segment number of the DATASEG ī‚ˇ To correct this mov ax,@data mov ds,ax ī‚ˇ The assembler translates@data into a segment number ī‚ˇ Two instructions are necessary since a number cannot be moved directly into a segment register So, back to printing a string... %TITLE "Print String Program -- PRTSTR.ASM" IDEAL MODEL small STACK 256 DATASEG msg DB "Hello!$" CODESEG Start:
  • 17. mov ax,@data ;Initialize DS to address mov ds,ax ; of data segment lea dx,[msg] ;get message mov ah,09h ;display string function int 21h ;display message Exit: mov ah,4Ch ;DOS function: Exit program mov al,0 ;Return exit code value int 21h ;Call DOS. Terminate program END Start ;End of program / entry point