Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. According to the Intel 8086 instruction set manual, MOV instruction can move data from these locations:
  2.  
  3. MOV REGISTER,REGISTER ; example mov ax,bx
  4. MOV REGISTER,IMMEDIATE ; example mov ax,5h
  5. MOV MEMORY,REGISTER ; example mov arr,ax
  6. MOV REGISTER,MEMORY ; example mov ax,arr
  7. MOV MEMORY,IMMEDIATE ; example mov arr,20h
  8.  
  9. REGISTER : means any register.
  10. MEMORY : means any byte (8-bit) or word (16-bit) in memory (variable).
  11. IMMEDIATE : means any immediate value (0Ah, 10d).
  12.  
  13. Kol Register in 8086 assembly lih est3maloh el 5as, here is what the standard mentions:
  14.  
  15. AX = Accumulator register, used to contain result of arithmetic equations, return values from subroutines, and I/O devices' data.
  16. BX = Base register, used to contain the base address of data structures.
  17. CX = Counter register, used to contain counter values in loops, and shifting values in bitwise operations.
  18. DX = Data register, like AX, used to contain result of arithmetic equations, and I/O devices' data.
  19.  
  20. IP = Instruction pointer, used to contain the address of the next instruction to be executed in the Code Segment.
  21. SP = Stack pointer, points at the top address of the stack.
  22. BP = Base pointer, points at the base of a stack frame.
  23.  
  24. SI = Source index, used to contain index values.
  25. DI = Destination index, used to contain index values.
  26.  
  27. CS = Code segment, used to contain the base address of the Code Segment (dih el 7eta ely fiha el code beta3 el bernameg beta3na ba3d mabyt3malo load fel memory).
  28. DS = Data segment, used to contain the base address of the Data Segment (dih el 7eta ely fiha el initialized data).
  29. SS = Stack segment, used to contain the base address of the region of memory used as a stack.
  30. ES = Extra data segment, used to contain the base address of a part of memory which has the uninitialized data, or more initialized data when DS is full.
  31.  
  32. FLAGS = flags register, used to contain el state beta3et el application beta3y, wel results of arithmetic and comparison instructions.
  33.  
  34. ---------------------------------------------------------------------------
  35. So now, nigi netaba2 el klam ely maktob fel standard 3ala sho3'lna.
  36.  
  37. shofna fel awel 5ales MOV instruction, MEMORY hena ma3naha ay accessible memory location, we katbna mabein parenthesis variable, ya3ny MEMORY hena means a variable.
  38. Data structures are considered variables too.
  39. An array is a data structure.
  40. Linked List is a data structure.
  41. A tree is a data structure.
  42. and Stack also is a data structure.
  43.  
  44. In assembly, 3alshan ne-access data structure, bena5od el base beta3et el structure dih into a register, we ben-access el elements wel members beto3ha men 5elal el DI wel SI.
  45. So, nestanteg men kda en el registers ely ne2dar nesta5demha m3a ba3d homa:
  46. BX with SI and DI.
  47. DS with SI and DI (sometimes BX could be used for large indexes).
  48. ES with SI and DI (sometimes BX could be used for large indexes).
  49.  
  50. Also, fel assembly, lama a3mel call le subroutine (same as function in C), wana gowa el function, ba2dar a-access el function parameters men 5elal el SP wel BP.
  51. So, nestanteg men kda en el registers ely ne2dar nesta5demha m3a ba3d homa:
  52. BP with SI and DI.
  53. DS with BP.
  54. ES with BP.
  55.  
  56. ----------------------------------------------------
  57. How parameters are passed to functions in assembly:
  58. ***************************************************
  59.  
  60. - hena hanwada7 isA ezay el BP is used to access function parameters in assembly.
  61. - In assembly, bady el parameters lel functions by pushing them into the stack abl ma-call el function beta3ty.
  62. - Let's take that assembly snippet of code as an example, hena hane3mel function esmaha mul_me(x,y), beta5od 2 parameters, we bet-return their multiplication:
  63.  
  64. mov cx,05h
  65. mov dx,10h
  66. push cx
  67. push dx
  68. call mul_me
  69. hlt
  70.  
  71. mul_me:
  72. push bp
  73. mov bp,sp
  74. mov bh,[bp+04h] ;bh now contains dx (10h)
  75. mov al,[bp+06h] ;al now contains cx (05h)
  76. mul bh ;AX = AL*BH
  77. pop bp
  78. ret
  79.  
  80.  
  81. - Now, zay ma7na shayfeen, 7atena values gowa el CX wel DX, we 3amalnalhom push into the Stack, we zay ma7na 3arfeen, stack pointer grows downward, bema3na en kol data bene3melaha push, el SP bey2el bel size ely 3amalnaloh push, hena e7na 3amlna push lel cx, fa beltaly el SP = SP-2 (since CX is a register, and registers are 2 bytes), we ba3dein 3amlna push lel dx, fabeltaly na2sna another 2 bytes men el SP, faba2et SP = SP-4.
  82.  
  83. law 7abena neshoof el stack 3amla ezay le7ad el line ely abl el CALL (lesa mad5lnash fel function), hanla2y el stack manzarha 3amel kda:
  84.  
  85. | |<- 0x0000
  86. | |
  87. | |
  88. |____________|<- SP points here, which is -4 from the top of the stack
  89. |____10h_____|
  90. |____05h_____|<- 0xFFFF (top of the stack)
  91. | |
  92. | |
  93.  
  94. ba3d man-execute el CALL instruction, CALL instruction automatically pushes the value of IP (instruction pointer) into the stack, we ba3d mad5lna fel function, 3amlna push lel BP 3alshan nesta3melha in accessing function parameters, tab3an momken nesta3'na 3an el 5atwa dih en e7na nesta3mel 3alatool SP 3alshan ne-access el parameters (CX and DX), bas fel assembly ben7awel man3'ayarsh el SP bekadr el mostata3 3alshan dih beyt3malaha increment we decrement automatically, fa3lshan manla5batsh el denya, we 3alshan man3melsh overwrite lel data beshakl mofage2 aw bel3'alat, fa bena5od el value beta3et el SP gowa el BP (mov bp,sp), we benet7akem bekol 7aga men 5elal el BP.
  95.  
  96. Now, ba3d awel 2 lines fel function, el stack manzarha kda:
  97.  
  98. | |<- 0x0000
  99. | |
  100. |____________|<- SP and BP points here now.
  101. |____BP______|
  102. |____IP______|<- If we want to access this word, BP+2h
  103. |____10h_____|<- If we want to access this word, BP+4h
  104. |____05h_____|<- If we want to access this word, BP+6h
  105. | |
  106. | |
  107.  
  108. So, here 3erefna ne-access el parameters men 5elal el BP register, fa3mlna mov lel parameters gowa el BH wel AL, we 3amalnalhom multiply, we ba3dein 3amlna pop el BP (since en e7na fy awel el function 3'ayarna el value beta3et el BP, fa lazem abl manetla3 menha nerag3ha lel value beta3et-ha ely 3amlnlha push on the stack).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement