c programming - assembly for beginners

Upload: sundarms

Post on 07-Apr-2018

226 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 C Programming - Assembly for Beginners

    1/7

    Rameshwar Saran

    Shakti Saranhttp://shaktisaran.tech.officelive.com/ProgrammingInC.aspx

    http://shaktisaran.tech.officelive.com/ProgrammingInC.aspxhttp://shaktisaran.tech.officelive.com/ProgrammingInC.aspx
  • 8/3/2019 C Programming - Assembly for Beginners

    2/7

    Answer this: If machines are getting exponentially more powerful andMoores law holds good then why software users aren't seeing similar

    increase in speed while running applications?

    This has two reasons:1. Additional processing power has been employed to support new

    features (such as graphics)2. Today's programmers don't take the time to consider how the

    underlying hardware shall operate for the code they write.

    Assembly language is a low level programming language, learning it will

    help you understand how the underlying hardware operates, which willenable you to write efficient software.

    Often, you'll hear old-time programmers make the comment that trulyefficient software is written in assembly language. The real reasonassembly language programs tend to be more efficient than programswritten in other languages is because assembly language forces theprogrammer to consider how the underlying hardware operates with eachmachine instruction they write.

  • 8/3/2019 C Programming - Assembly for Beginners

    3/7

    You need to get some knowledge about computer structure in order tounderstand assembly language.

    CPU - the heart of the computer, most of computations occur inside CPU.RAM - programs are loaded here in order to be executed.SYSTEM BUS (shown in yellow) - connects the various components of acomputer.

  • 8/3/2019 C Programming - Assembly for Beginners

    4/7

    Main registers (General Purpose Registers)

    AH AL AX (primary accumulator)

    BH BL BX (base, accumulator)CH CL CX (counter, accumulator)

    DH DL DX (accumulator, other functions)

    Index registers (General Purpose Registers)

    SI Source Index

    DI Destination Index

    BP Base Pointer

    SP Stack Pointer

    Status register

    15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 (bit position)

    - - - - O D I T S Z - A - P - C Flags

    Segment register

    CS Code Segment

    DS Data Segment

    ES ExtraSegment

    SS Stack Segment

    Instruction pointer

    IP Instruction Pointer

    General Purpose Registers: The 8086 had eight 16-bit registers including the stack pointer,but excluding the instruction pointer, flag register and segment registers. Four of them(AX,BX,CX,DX) could also be accessed as 8-bit registers (AH,AL,BH,BL, etc), the other four(BP,SI,DI,SP) were 16-bit only.Despite the name of a register, it's the programmer who determines the usage for eachgeneral purpose register. The main purpose of a register is to keep a number (variable) e.g.

    something like: 0011000000111001b in binary, or 12345 in decimal.

  • 8/3/2019 C Programming - Assembly for Beginners

    5/7

    A 20-bit external address bus gave an 1 MB (segmented) physical address space (220 =1,048,576).The data bus was multiplexed with the address bus in order to fit a standard 40-pin dual in-line package. (Multiplexing: Digital data streams are combined into one signal over a shared

    medium. The aim is to share an expensive resource. For example, in telephony, severalphone calls may be transferred using one wire.)

    16-bit I/O addresses meant 64 KB of separate I/O space (216 = 65,536).Because internal registers were only 16 bits wide the maximum linear address space waslimited to 64 KB. Programming over 64 KB boundaries involved adjusting segment registers

    (and remained so until the 80386).

    All internal registers as well as internal and external data buses were 16 bits wide, firmly

    establishing the "16-bit microprocessor" identity of the 8086.

    http://en.wikipedia.org/wiki/File:Wyprowadzenie_mikroprocesora_8086.JPG
  • 8/3/2019 C Programming - Assembly for Beginners

    6/7

    Segment Registers: They have a very special purpose - pointing at accessible blocks ofmemory (although it is possible to store any data in the segment registers, this is never a

    good idea).CS - points at the segment containing the current program.DS - generally points at segment where variables are defined.ES- extra segment register, it's up to a coder to define its usage.SS - points at the segment containing the stack.

    To access any memory location segment registers work together with general purposeregister. For example, to access memory at the physical address 12345h , we set the DS =1230h and SI = 0045h. This way we can access much more memory than with a singleregister that is limited to 16 bit values (64 KB).

    CPU makes a calculation of physical address by multiplying the segment register by 10h andadding general purpose register to it (1230h * 10h + 45h = 12345h):

    The address formed with 2 registers is called an effective address. By default BX, SI andDI registers work with DS segment register; BP and SP work with SS segment register.Other general purpose registers cannot form an effective address! Also, although BX canform an effective address, BH and BL cannot.

    IP (instruction pointer) register always works together with CS segment register and it pointsto currently executing instruction.

  • 8/3/2019 C Programming - Assembly for Beginners

    7/7

    Segmentthe value in segment register (CS, DS, SS, ES) is called a segment

    Offsetand the value in purpose register (BX, SI, DI, BP) is called an offset.

    When DS contains value 1234h and SI contains the value 7890h it can be alsorecorded as 1234:7890.

    The physical address will be 1234h * 10h + 7890h = 19BD0h.

    Note:

    If zero is added to a decimal number it is multiplied by 10.However 10h = 16, so if zero is added to a hex value, it is multiplied by 16. Forexample:

    7h = 770h = 112