Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Assembly language part I


Published on

This presentation covers very basics of assembly language with some computer organization concept. I took this session as part of on going series on assembly at NULL Hyderabad meets. PART II will cover instruction sets and more in detail.

Published in: Technology

Assembly language part I

  1. 1. Assembly LanguageBy Mohammed ImranGet your hands dirty withPART- I@imran_naseem
  2. 2. If IsayAssemblyiscool!
  3. 3. Seriously ?Peoplesay
  4. 4. Andsomegoevenfurther..
  5. 5. ButyoucoulddosomeAmazingthings
  6. 6. CreatefasterprogramsNo, you cant fly cars with assembly :)
  7. 7. Flythese
  8. 8. Letsstart
  9. 9. CPUcantunderstandc,javaorassembly.
  10. 10. hencewehavecompilers,assemberstoconverthighlevelcodetomachinecode.
  11. 11. Hello.c#include <stdio.h>#define STRING "Hello World"int main(void){/* Using a macro to print Hello World*/printf(STRING);return 0;}
  12. 12. Youcanseetheseintermediatestagesusinggcccommand
  13. 13. gcccommandsgcc -Wall -save-temps hello.c -o helloThe above command saves temporary files generated duringcreation of binary file hello in the current directory
  14. 14. ls hello.*hello.i ;Preprocessedfilehello.s ;assemblyfilehello.o ;objectfilehello ;binaryfile
  15. 15. Demo
  16. 16. An assembly language is a low-levelprogramming language for a computer, orother programmable device, in which there isa very strong (generally one-to-one)correspondence between the language andthe architectures machine code instructions.“”WhatisAssemblylanguage?
  17. 17. Assembly is easy to learn, but hard to master!“ ”
  18. 18. AssemblyactsasbridgeMachine Language High level languageAssembly Language
  19. 19. Machinecode10110000 01100001This is how, an instructionin machine language looks like
  20. 20. Andcodeisparsedlike.10110000 01100001Instruction Register Register/Operand
  21. 21. Machinecodeinhex10110000 01100001B0 61 (in hex)The above machine code representedIn hexadecimal format for ease.
  22. 22. Assemblyrepresentation10110000 01100001B0 61 (in hex)MOV AL, 61hThe above machine code representedIn assembly language code
  23. 23. MOV AL, 61h ; Load AL with 97 decimal (61 hex)Whatdoesitmean?Opcodes Operands
  24. 24. Letsseehowitallfitstogether
  25. 25. SystemorganizationCPUMemoryIOBus
  26. 26. CPUcontainsregisters,flagsandALUtodomathoperations.
  27. 27. TypicalCPUContentsArithmetic andLogical UnitRegistersflagsSegment registersCPU
  28. 28. RegistersarelikevariablesinC,usedtostoreandcomputedatatemporarily.
  29. 29. RegistersSPSI DIAX, BX, CX, DXIPGeneral-purpose registers for storing numbers.
  30. 30. RegistersSPSI DIAX, BX, CX, DXIPGeneral-purpose registers for storing numbers.Source and destination data index registers.memory pointers for retrieving and storingdata.
  31. 31. RegistersSPSI DIAX, BX, CX, DXIPGeneral-purpose registers for storing numbers.Source and destination data index registers.memory pointers for retrieving and storingdata.Stack pointer, used to store parameters andvariables on the stack.
  32. 32. RegistersSPSI DIAX, BX, CX, DXIPGeneral-purpose registers for storing numbers.Source and destination data index registers.memory pointers for retrieving and storingdata.Stack pointer, used to store parameters andvariables on the stack.Instruction pointer, points to next instructionto execute.
  33. 33. Alsodependingonthecpuarch,theregisternameandsizevaries.
  34. 34. RegisterssizesAX, BX, CX, DX 16 bit CPU Architecture32 bit CPU ArchitectureEAX, EBX, ECX, EDXRAX, RBX, RCX, RDX 64 bit CPU Architecture
  35. 35. ForHandlingspecialsectionswehavesegmentregisters
  36. 36. SegmentsCode Segment(CS)Place where assembly code is storedData Segment(DS)Stack Segment(SS)Extra Segment(ES)Place where initialized data is storedPlace where stack data is storedPlace kept for extra data handling
  37. 37. Memorymanagement
  38. 38. Everyprocessinmemorythinksitstheonlyprocessinthesystem
  39. 39. Memoryislaidoutinphysicalramaccordingtovirtualmemory.
  40. 40. Virtualmemorymodel1234h4567h1234h4567hProcess1Process2Process3
  41. 41. to store codePlace to store initialized dataPlace to store un initialized dataPlace to store dynamic dataPlace to store func variables & params
  42. 42. Alsoweneedtounderstandhowstackworks
  43. 43. Alsoweneedtounderstandhowstackworks
  44. 44. Stack(LIFO)Lower AddressHigher AddressGrows DownwardsESP0x123456780x12345690
  45. 45. Stack(LIFO)-PushLower AddressHigher AddressPush ABCDEF00ESP0x123456780x123456900xABCDEF00
  46. 46. Stack(LIFO)-PushLower AddressHigher AddressPush ABCDEF00ESP0x123456780x123456900xABCDEF00ESP = ESP-1
  47. 47. Stack(LIFO)-POPLower AddressHigher AddressPOPESP0x123456780x123456900xABCDEF00
  48. 48. Stack(LIFO)-POPLower AddressHigher AddressPOPESP0x123456780x12345690ESP = ESP+1
  49. 49. Instructionset● Mov● Add/sub/multiply/divide● cmp● Jmp/jne/jz/je/jnz/jg/jl● int
  50. 50. Movestatement● Move statement moves data from one placeto another
  51. 51. BeforeandAfterMoveBefore MoveAfter MoveAX=30h BX=10hAX=10h BX=10hAX=10h BX=10hINSTRUCTION: MOV AX,BX
  52. 52. ExamplesMove AX, BX ; move bx content to AXMove AL, 06h ; move 06h into ALMove AX, [BX]; If BX=90, move contentpresent in memory 90 to AX.
  53. 53. Add/Sub/Multiply/Divide● Adds, subtracts, multiplies and divides thenumbers and stores it in the AX registers andthese instructions can affects flags.
  54. 54. Examples● Add AX,05h – Add 05h to AX and stores resultback in AX● Add AX,BX - Add contents of BX and AX, storeresult in AX. Affects flags● Sub AX,05h – Subtract 05h from AX, storeresult in AX. Affects flags
  55. 55. Comparestatement● Cmp CX,05h – Compare CX with 05h, resultswill be reflected in special registers calledflags.
  56. 56. Examples● Cmp CX, 05h; if cx=2, then Negative flag isset.
  57. 57. Jumpinstructions● Jump to a different part of the code.● If label is given then jumps to label section● Conditional jump happens based on flags.
  58. 58. Examples● Jnz loop; jump to label loop if zero flag is notzero● Jmp loop; jump to label loop part of the code● Jz loop ; jump to label loop if zero flag is set
  59. 59. Interruptinstruction● Interrupts the CPU and jumps to the locationgiven.
  60. 60. ExamplesInt 21h; calls the 21h OS routine
  61. 61. TobecontinuedinpartII...Part- II will cover Instruction set and other concepts in depth.This presentation is/was a teaser for the part II
  62. 62. Credits●●● All the icons are from The noun project● Assembly language primer for