Assembly Language Fundamentals of Assembly language Motaz K. Saad Spring 2007 Motaz K. Saad, Dept. of CS
Review of existing concepts Comments Directives (page, segment, title) Data type (Byte (DB), Word(DW), Doubleword(DD), String Some arithmetic operations: ADD,SUB,MUL,DIV Motaz K. Saad, Dept. of CS
Operand Types Three basic types of operands: Immediate – a constant integer (8, 16, or 32 bits) value is encoded within the instruction Register – the name of a register register name is converted to a number and encoded within the instruction Memory – reference to a location in memory memory address is encoded within the instruction, or a register holds the address of a memory location Motaz K. Saad, Dept. of CS
Data transfer instructions MOV instruction Move from source to destination. Syntax: MOV  destination,source Transfers data referenced by the address of the second operand to the address of the first operand Destination has to have the same length as source [label:] MOV  register/memory  register/memory/immediate Example: MOV F, AX ; // Move content of AX to the variable F MOV CX, D ;// Move value of D to CX MOV ES, AX MOV AX, 215 Motaz K. Saad, Dept. of CS
MOV Instruction .data count DB 100 wVal  DW 2 .code mov bl,count mov ax,wVal mov count,al mov al,wVal ; error mov ax,count ; error mov eax,count ; error Move from source to destination. Syntax: MOV  destination,source No more than one memory operand permitted MOV  VAR1,VAR2  CS, IP, and IP cannot be the destination  MOV IP, 100 No immediate to segment moves MOV DS, @DATA Motaz K. Saad, Dept. of CS
Load Effective Address.  REG = address of memory (offset)  LEA register/memory Example: LEA AX, m  ;load offset address of m to AX LEA instruction Motaz K. Saad, Dept. of CS
Arithmetic instructions INC and DEC instruction Increasing or decreasing the contents of register or memory location by 1 INC/DEC register/memory Flag: OF, SF and ZF OF:is set when an instruction resulted in a carry into the sign bit of the result.  SF: is set if the sign bit of a result is set  ZF: is set if the result is equal to 0.  Motaz K. Saad, Dept. of CS
Arithmetic instructions ADD ADD/SUB operand1, operand 2 operand1 =operand 1 + operand 2 Operand 1: register/memory Operand 2: register/memory/immediate Motaz K. Saad, Dept. of CS
Arithmetic instructions SUB SUB operand1, operand 2 operand1 =operand 1 - operand 2 operand 1: register/memory operand 2: register/memory/immediate Motaz K. Saad, Dept. of CS
Arithmetic instructions MUL operand Unsigned multiply.  Operand: register/memory Motaz K. Saad, Dept. of CS
Arithmetic instructions IMUL operand Signed multiply.  Operand: register/memory Example: MOV AX, -2 MOV CX, -3 IMUL CX ;  AX = +6 CF = 0  Motaz K. Saad, Dept. of CS
Arithmetic instructions DIV operand Unsigned multiply.  Operand: register/memory when operand is a  byte : AL = AX / operand AH = remainder (modulus)  when operand is a  word : DX = remainder (modulus) Motaz K. Saad, Dept. of CS
Arithmetic instructions IDIV operand Signed multiply.  Operand: register/memory when operand is a  byte : AL = AX / operand AH = remainder (modulus)  when operand is a  word : DX = remainder (modulus) Motaz K. Saad, Dept. of CS
Write a program to convert from  Celsius to Fahrenheit  and vice versa : Tc = (5/9)*(Tf-32)  Tc: censius Tf: fahrenheit ( The result may not be accurate due to the integer division but that is fine) Practice Motaz K. Saad, Dept. of CS
Repetitive move instructions copying a string to another TITLE A04ASM1 (EXE)  Move and add operations ; --------------------------------------------- .STACK ; ---------------------------------------------- .DATA STRING1 DB "12345678","$" STRING2 DB ? Motaz K. Saad, Dept. of CS
Repetitive move instructions .CODE MAIN PROC  FAR MOV AX, @DATA MOV DS, AX MOV ES, AX MOV CX, 09  ; Initialize to move 9 characters LEA SI, STRING1  ; Initialize source index register to offset of string 1  LEA DI, STRING2  ; Initialize destination index register to offset of string 2 BEGINLOOP: MOV AL,[SI]  ; Get a current character from string 1 to AL MOV [DI], AL  ; Move it to the current character in string 2 INC SI  ; Move to the next character in string 1 INC DI  ; Move to the next character in string 2 DEC CX  ; Decrease the count for loop JNZ  BEGINLOOP  ; Continue to loop if count is not 0 MOV AH, 09H LEA DX, STRING2 int 21H  ; Display String 2 .EXIT MAIN ENDP ;End of procedure END   MAIN ;End of program Motaz K. Saad, Dept. of CS
Repetitive move instructions DEC CX ZF = 1 if CX = 0 JNZ LABEL if ZF = 0 then jump to the label Motaz K. Saad, Dept. of CS
Practice Develop an assembly program to: Define byte items: BYTE1 and BYTE2 (Assign any values for these two variables) Define a word item: WORD3 and WORD3=0 Move content of Byte1 to AL Add content of Byte2 to AL Set DL= 42H Exchange the content of AL and DL Multiply the contents of AL by DL Transfer product from AX to WORD3 Motaz K. Saad, Dept. of CS
Addressing mode Register addressing:  E.g ADD AX, BX fastest type of operations Immediate addressing Immediate contains a constant value or an expression E.g:  MOV AX, 0245H Direct memory addressing One of operand references a memory location and the other operand references a register E.G  MOV FLDF, AX Motaz K. Saad, Dept. of CS
Addressing mode Direct-Offset addressing use arithmetic instruction to modify an address e.g MOV CX, DATAZ+2 Indirect memory addressing Use BX and BP, DI and SI within [ ] e.g.  MOV [BX], CL Motaz K. Saad, Dept. of CS
Addressing mode Base Displacement Addressing Uses BX, BP and DI, SI and combine with a displacement to form an effective address E.g   MOV AL,[SI+2] Base-Index Addressing Combine BX,BP with DI,SI to form effective address E.G MOV AL,[BX+SI] Motaz K. Saad, Dept. of CS
Addressing mode Base-Index Displacement Addressing Combine BX, BP and DI, SI and a displacement to form an effective address E.g   MOV AL,[BX+SI+2] Motaz K. Saad, Dept. of CS

Assembly Language Lecture 4

  • 1.
    Assembly Language Fundamentalsof Assembly language Motaz K. Saad Spring 2007 Motaz K. Saad, Dept. of CS
  • 2.
    Review of existingconcepts Comments Directives (page, segment, title) Data type (Byte (DB), Word(DW), Doubleword(DD), String Some arithmetic operations: ADD,SUB,MUL,DIV Motaz K. Saad, Dept. of CS
  • 3.
    Operand Types Threebasic types of operands: Immediate – a constant integer (8, 16, or 32 bits) value is encoded within the instruction Register – the name of a register register name is converted to a number and encoded within the instruction Memory – reference to a location in memory memory address is encoded within the instruction, or a register holds the address of a memory location Motaz K. Saad, Dept. of CS
  • 4.
    Data transfer instructionsMOV instruction Move from source to destination. Syntax: MOV destination,source Transfers data referenced by the address of the second operand to the address of the first operand Destination has to have the same length as source [label:] MOV register/memory register/memory/immediate Example: MOV F, AX ; // Move content of AX to the variable F MOV CX, D ;// Move value of D to CX MOV ES, AX MOV AX, 215 Motaz K. Saad, Dept. of CS
  • 5.
    MOV Instruction .datacount DB 100 wVal DW 2 .code mov bl,count mov ax,wVal mov count,al mov al,wVal ; error mov ax,count ; error mov eax,count ; error Move from source to destination. Syntax: MOV destination,source No more than one memory operand permitted MOV VAR1,VAR2 CS, IP, and IP cannot be the destination MOV IP, 100 No immediate to segment moves MOV DS, @DATA Motaz K. Saad, Dept. of CS
  • 6.
    Load Effective Address. REG = address of memory (offset) LEA register/memory Example: LEA AX, m ;load offset address of m to AX LEA instruction Motaz K. Saad, Dept. of CS
  • 7.
    Arithmetic instructions INCand DEC instruction Increasing or decreasing the contents of register or memory location by 1 INC/DEC register/memory Flag: OF, SF and ZF OF:is set when an instruction resulted in a carry into the sign bit of the result. SF: is set if the sign bit of a result is set ZF: is set if the result is equal to 0. Motaz K. Saad, Dept. of CS
  • 8.
    Arithmetic instructions ADDADD/SUB operand1, operand 2 operand1 =operand 1 + operand 2 Operand 1: register/memory Operand 2: register/memory/immediate Motaz K. Saad, Dept. of CS
  • 9.
    Arithmetic instructions SUBSUB operand1, operand 2 operand1 =operand 1 - operand 2 operand 1: register/memory operand 2: register/memory/immediate Motaz K. Saad, Dept. of CS
  • 10.
    Arithmetic instructions MULoperand Unsigned multiply. Operand: register/memory Motaz K. Saad, Dept. of CS
  • 11.
    Arithmetic instructions IMULoperand Signed multiply. Operand: register/memory Example: MOV AX, -2 MOV CX, -3 IMUL CX ; AX = +6 CF = 0 Motaz K. Saad, Dept. of CS
  • 12.
    Arithmetic instructions DIVoperand Unsigned multiply. Operand: register/memory when operand is a byte : AL = AX / operand AH = remainder (modulus) when operand is a word : DX = remainder (modulus) Motaz K. Saad, Dept. of CS
  • 13.
    Arithmetic instructions IDIVoperand Signed multiply. Operand: register/memory when operand is a byte : AL = AX / operand AH = remainder (modulus) when operand is a word : DX = remainder (modulus) Motaz K. Saad, Dept. of CS
  • 14.
    Write a programto convert from Celsius to Fahrenheit and vice versa : Tc = (5/9)*(Tf-32) Tc: censius Tf: fahrenheit ( The result may not be accurate due to the integer division but that is fine) Practice Motaz K. Saad, Dept. of CS
  • 15.
    Repetitive move instructionscopying a string to another TITLE A04ASM1 (EXE) Move and add operations ; --------------------------------------------- .STACK ; ---------------------------------------------- .DATA STRING1 DB "12345678","$" STRING2 DB ? Motaz K. Saad, Dept. of CS
  • 16.
    Repetitive move instructions.CODE MAIN PROC FAR MOV AX, @DATA MOV DS, AX MOV ES, AX MOV CX, 09 ; Initialize to move 9 characters LEA SI, STRING1 ; Initialize source index register to offset of string 1 LEA DI, STRING2 ; Initialize destination index register to offset of string 2 BEGINLOOP: MOV AL,[SI] ; Get a current character from string 1 to AL MOV [DI], AL ; Move it to the current character in string 2 INC SI ; Move to the next character in string 1 INC DI ; Move to the next character in string 2 DEC CX ; Decrease the count for loop JNZ BEGINLOOP ; Continue to loop if count is not 0 MOV AH, 09H LEA DX, STRING2 int 21H ; Display String 2 .EXIT MAIN ENDP ;End of procedure END MAIN ;End of program Motaz K. Saad, Dept. of CS
  • 17.
    Repetitive move instructionsDEC CX ZF = 1 if CX = 0 JNZ LABEL if ZF = 0 then jump to the label Motaz K. Saad, Dept. of CS
  • 18.
    Practice Develop anassembly program to: Define byte items: BYTE1 and BYTE2 (Assign any values for these two variables) Define a word item: WORD3 and WORD3=0 Move content of Byte1 to AL Add content of Byte2 to AL Set DL= 42H Exchange the content of AL and DL Multiply the contents of AL by DL Transfer product from AX to WORD3 Motaz K. Saad, Dept. of CS
  • 19.
    Addressing mode Registeraddressing: E.g ADD AX, BX fastest type of operations Immediate addressing Immediate contains a constant value or an expression E.g: MOV AX, 0245H Direct memory addressing One of operand references a memory location and the other operand references a register E.G MOV FLDF, AX Motaz K. Saad, Dept. of CS
  • 20.
    Addressing mode Direct-Offsetaddressing use arithmetic instruction to modify an address e.g MOV CX, DATAZ+2 Indirect memory addressing Use BX and BP, DI and SI within [ ] e.g. MOV [BX], CL Motaz K. Saad, Dept. of CS
  • 21.
    Addressing mode BaseDisplacement Addressing Uses BX, BP and DI, SI and combine with a displacement to form an effective address E.g MOV AL,[SI+2] Base-Index Addressing Combine BX,BP with DI,SI to form effective address E.G MOV AL,[BX+SI] Motaz K. Saad, Dept. of CS
  • 22.
    Addressing mode Base-IndexDisplacement Addressing Combine BX, BP and DI, SI and a displacement to form an effective address E.g MOV AL,[BX+SI+2] Motaz K. Saad, Dept. of CS