it153l introduction to assembly language revised

Upload: jocansino4496

Post on 04-Apr-2018

225 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    1/95

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    2/95

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    3/95

    Fourth: LoaderBuilt-in to the operating system and is never explicitlyexecuted.Takes the relocatable code created by the linker,

    loads: it into memory at the lowest available location,then runs it. Fifth: Debugger

    Environment for running and testing assemblylanguage programs.

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    4/95

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    5/95

    DOS provides the environment in which programs run. Provides a set of helpful utility functions

    Must be understood in order to create program in

    DOS

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    6/95

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    7/95

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    8/95

    Assembly language Thought goes into the use of the computer memory

    and the CPU registersRegister Like a memory location in that it can store a byte

    (or work) value. No address in the memory, it is not part of the

    computer memory(built into the CPU)

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    9/95

    Importance of Registers in Assembly Prog. Instructions using registers > operating on values

    stored at memory locations. Instructions tend to be shorter (less room to store

    in memory) Register-oriented instructions operate faster that

    memory-oriented instructionsSince the computer hardware can access a registermuch faster than a memory location.

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    10/95

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    11/95

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    12/95

    IP InstructionPointer

    16-bit numberthat points to theoffset of the nextinstruction

    SP Stack Pointer

    16-bit numberthat points to theoffset that the

    stack is using

    BP Base Pointerused to pass datato and from thestack

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    13/95

    AX AccumulatorRegister

    mostly used forcalculations andfor input/output

    BX Base RegisterOnly register thatcan be used as anindex

    CX Count Registerregister used forthe loop

    instruction

    DX Data Registerinput/output andused by multiplyand divide

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    14/95

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    15/95

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    16/95

    Consist of 9 status bits(flags)Flags because it can be either SET(1) NOT SET(0)

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    17/95

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    18/95

    Zero Flag 6if set , resultingnumber of calculation is zero

    Auxiliary Carry 4 some sort of second carry flag

    Parity Flag 2 indicates even orodd parity

    Carry Flag 0contains the left-most bit aftercalculations

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    19/95

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    20/95

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    21/95

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    22/95

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    23/95

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    24/95

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    25/95

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    26/95

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    27/95

    The source code can only be assembled by anassembler or and the linker. A86 MASM TASM we will use this oneInstall TASMThen use the tasm.exe and tlink.exe

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    28/95

    How to Assemble

    The Assemble To assemble Type the ff. on the command prompt:

    cd c:\tasm\bin tasm

    tasm c:\first.asm tlink

    tlink c:\tasm\bin\first.obj or tlink first.obj

    To run call the .exe on the command prompt: Example in our program(First.asm) C:\tasm\bin\first.exe or just first.exe

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    29/95

    .model small

    .stack

    .data message db "Hello world, I'm learning Assembly !!!", "$"

    .code

    main proc

    mov ax,seg message mov ds,ax

    mov ah,09lea dx,message

    int 21h

    mov ax,4c00hint 21h

    main endpend main

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    30/95

    Lines that start with a "." are used to provide the assembler with

    information. The word(s) behind it say what kind of info.

    In this case it just tells the assembler that the program is small anddoesn't need a lot of memory. I'll get back on this later.

    This one tells the assembler that the "stack" segment starts here.

    The stack is used to store temporary data.

    indicates that the data segment starts here and that the stack

    segment ends there.

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    31/95

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    32/95

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    33/95

    Memory space for variables DB (Byte 8 bit ) DW (Word 16 bit) DD (Doubleword 32 bit) Example:

    foo db 27 ;by default all numbers are decimal bar dw 3e1h ; appending an "h" means hexadecimal

    real_fat_rat dd ? ; "?" means "don't care about the value Variable name

    Address cant be changed Value can be changed

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    34/95

    .model small

    .stack

    .data message db "Hello world, I'm learning Assembly !!!", "$"

    .code

    main proc

    mov ax, seg message mov ds,ax

    mov ah,09lea dx,messageint 21h

    mov ax,4c00hint 21h

    main endp

    end main

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    35/95

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    36/95

    Syntax: MOV destination , source

    Allows you to move data into and out the

    registers Destination

    either registers or mem. Loc. Source

    can be either registers, mem. Loc. or numeric value

    Memory-to-memory transfer NOT ALLOWED

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    37/95

    foo db 27 ;by default all numbers are decimal

    bar dw 3e1h ; appending an "h" means hexadecimalreal_fat_rat dd ? ; "?" means "don't care about the value

    mov ax,bar ; load the word-size register ax with; the word value stored at location bar.

    mov dl,foo ; load the byte-size register dl with; the byte value stored at location foo.

    mov bx,ax ; load the word-size register bx with; the byte value in ax.

    mov bl,ch ; load the byte-size register bl with; the byte value in ch.

    mov bar,si ; store the value in the word-size; register si at the memory location; labelled "bar".

    mov foo,dh ; store the byte value in the register; dh at memory location foo.

    mov ax,5 ; store the word 5 in the ax register. mov al,5 ; store the byte 5 in the al register. mov bar,5 ; store the word 5 at location bar. mov foo,5 ; store the byte 5 at location foo.

    Codes we do earlier

    Notice the size of the source and

    destination(must match inreg-reg,

    mem-reg,reg-memTransfers)

    Constant mustconsistent with the

    destination

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    38/95

    MOV AL, 3172 MOV foo, 3172

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    39/95

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    40/95

    Here it moves the number in the AX register (the number of the

    data segment) into the DS register. We have to load this DS register this way (with two instructions) Just typing: "mov ds,segment message" isn't possible.

    MOV again. This time it load the AH register with the constant

    value nine.

    LEA - Load Effective Address.This instructions stores the offset within the datasegment of the bit-string message into the DX register.This offset is the second thing we need to know, when we want to knowwhere "message" is in the memory.So now we have DS:DX.

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    41/95

    BusControl

    Unit

    Instruction Pointer

    .model small

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    42/95

    .stack

    .data message db "Hello world, I'm learning Assembly !!!",

    "$"

    .code

    main proc mov ax,seg message mov ds,ax

    mov ah,09lea dx,messageint 21h

    mov ax,4c00hint 21h

    main endpend main

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    43/95

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    44/95

    After running: Go to DOS and type debug FIRST.exe to debug. Type d -> display some addresses Type u -> you will see something

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    45/95

    0F77:0000 B8C813 MOV AX,13C80F77:0003 8ED8 MOV DS,AX0F77:0005 B409 MOV AH,09

    Segment Number & Offset

    Machine Code instruction

    originally:->mov ax

    ->number

    It means that data is store in the segment with number 0F79

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    46/95

    The other instruction turnedinto . So that means that the offset of the bit-string is 0 -

    -> 13C8:0000. Try to type

    Calculating other addressWe will subtract 2 segments from 13C8 = 13C62 segments = 32 bit (0002:0000)The other address is

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    47/95

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    48/95

    he stack is a place where data is temporarilystoredThe SS and SP registers point to that placelike this: SS:SP So the SS register is the segment and the SP register

    contains the offsetThere are a few instructions that make use of the stack - Push a value on the stack - retrieve that value from the stack

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    49/95

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    50/95

    MOV AX, 1234H MOV BX, 5678H

    PUSH AXPOP BX

    We pushed the AX to the stack and we popped that value in BX.

    What is the final value of AX and BX?

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    51/95

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    52/95

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    53/95

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    54/95

    If you fully understand this stuff ( registers ,flags , segments , stack , names , etc.) you may,from now on, call yourself a

    http://www.xs4all.nl/~smit/asm01001.htmhttp://www.xs4all.nl/~smit/asm01001.htmhttp://www.xs4all.nl/~smit/asm01001.htmhttp://www.xs4all.nl/~smit/asm01001.htmhttp://www.xs4all.nl/~smit/asm01001.htmhttp://www.xs4all.nl/~smit/asm01001.htmhttp://www.xs4all.nl/~smit/asm01001.htmhttp://www.xs4all.nl/~smit/asm01001.htmhttp://www.xs4all.nl/~smit/asm01001.htmhttp://www.xs4all.nl/~smit/asm01001.htm
  • 7/30/2019 It153l Introduction to Assembly Language Revised

    55/95

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    56/95

    Assembly Version; destination variables play db ? misty db ?

    for db ? me db ?

    ; source variables my db 4

    name db 5is db 6nobody db 32.....

    mov al,my ; PLAY=MY mov play,al mov al,name ; MISTY=NAME mov misty,al mov al,is ; FOR=IS mov for,al mov al,nobody ; ME=NOBODY mov me,al

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    57/95

    We can write program in DEBUG The reason for this is that with DEBUG we can

    concentrate our thoughts purely on assemblylanguage

    DEBUG System Debugger Has its own built-in editor and primitive assembler Its code does not need to be linked also has facilities for modifying memory locations

    and for examining memory locations

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    58/95

    Debug cannot be used to conveniently develop larger

    programs one must literally know the memory addresses of

    all data items. an (immediate) value is distinguished from the

    value stored at an address in that an address is enclosed in square brackets.

    MOV AX, 200 load ax with the value 200MOV AX, [200] load ax with the value at address 200200 means 200H or 512

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    59/95

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    60/95

    The program may be entered with the "A" or "assemblecommand followed by the address. (A nnnn )-a10048EE:0100 mov ax,[200]48EE:0103 mov [204],ax

    48EE:0106 mov ax,[201]48EE:0109 mov [205],ax48EE:010C mov ax,[202]48EE:010F mov [206],ax48EE:0112 mov ax,[203]48EE:0115 mov [207],ax48EE:0118

    Entering a blank line terminates this process

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    61/95

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    62/95

    We can check that the program is actually in the computer at

    address 100 with the "U" or "unassemble" command.

    You may also type in tospecify the ending line to view

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    63/95

    MOV AX,[200] assembler

    RAM

    A10002

    U commandUnassembler

    Deduced CodeMOV AX,[200]

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    64/95

    Executable Program Loader

    RAM

    A10002

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    65/95

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    66/95

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    67/95

    View entered values using d or display command dnnn display from address nnnn dnnn,mmmm - display from nnnn to mmmm

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    68/95

    Running the program Using G or Go command

    G=nnnn,mmmm runs the program from address nnnn to mmmmIn our case:

    G=100,118 Verify if it really works by displaying the location 200 to 207

    -d200,207419F:0200 04 05 06 20 72 65 63 74 .......

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    69/95

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    70/95

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    71/95

    Terminating Debugger Q or Quit Command

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    72/95

    Other DEBUG command : R register command

    Examine or modify registers

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    73/95

    Modifying RegistersRrn where rn is the name of the registers(AX,BX...)Ex. to store 4567 (hex) in the CX register

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    74/95

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    75/95

    The ADD (SUB) instruction adds (subtracts) thevalue of the source operand to (from) the value of the destination operand, and stores the result in thedestination

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    76/95

    add dx,dx ; add the DX register to itself.add cx,5 ; add the value 5 to the cx reg.add si,di ; add the di register to si reg.add bl,cl ; add cl reg. to bl. reg.

    add foo,5 ; add the value 5 to the; variable foo.add foo,al ; add contents of al to foo.sub bar,5 ; subtract word value 5 from bar

    sub bar,3e1h ; subtract 3e1h from variable barsub al,foo ; subtract value of var. foofrom al

    sub si,ax ; subtract contents of ax from si

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    77/95

    Why the following codes are illegal

    add cl,3e1hadd cl,bxsub foo,cx

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    78/95

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    79/95

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    80/95

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    81/95

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    82/95

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    83/95

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    84/95

    It can happen in integer addition that theresult of an addition is too big for thedestination address to hold the carry flag is used to store both carries and

    borrows in integer addition and subtraction Ex:

    MOV AL,200 MOV BL,195 MOV CL,25 ADD AL,BL

    the carry flag would be "set" to one, and the resultwould be truncated to 8 bits: i.e., AL would contain139.

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    85/95

    MOV AL,200 MOV BL,195 MOV CL,25 ADD AL,CL

    the result, 225 (

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    86/95

    ADC ADC destination,source "add with carry ADC automatically adds in the carry left over

    from previous operationsSBB SBB destination,source "subtract with borrow

    SBB automatically subtracts the borrow

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    87/95

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    88/95

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    89/95

    The control is accomplished with "jump"instructions. Jump instructions have the syntax

    mnemonic address

    The mnemonic here can be a number of differentthings, but for the moment, we will assume that it is"JMP".

    A JMP instruction "jumps from the present location inmemory (as indicated by the instruction pointerregister IP) to the specified address in memory. Inessence, JMP simply stores the given address in the IPregister.

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    90/95

    In DEBUG, the address operand is, of course,simply a number.For example, if we executed the instruction JMP 121

    then the very next instruction executed wouldbe the instruction located at address 121h.

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    91/95

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    92/95

    JMP performs an unconditional jump: it always goes to the specified address, regardless

    of any special conditions that may obtain.There are also a series of conditional jump

    instructions which perform a jump only if some special condition is met. These instructions all have the general syntax given

    above, but their

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    93/95

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    94/95

  • 7/30/2019 It153l Introduction to Assembly Language Revised

    95/95