Posted By

oronm on 07/05/11


Tagged


Versions (?)

linked list


 / Published in: Assembler
 

  1. TITLE Linked list example
  2.  
  3. .386
  4. .model flat
  5.  
  6. extern _ExitProcess@4:Near
  7.  
  8. .data
  9. Linked DWORD Rabecca
  10.  
  11. Rabecca DWORD Abraham
  12. BYTE 'Rabecca',0
  13. Abraham DWORD Jacob
  14. BYTE 'Abraham',0
  15. Jacob DWORD 0
  16. BYTE 'Jacob',0
  17.  
  18. Itzhak DWORD ?
  19. BYTE 'Itzhak',0
  20.  
  21. Sarah DWORD ?
  22. BYTE 'Sarah',0
  23.  
  24. Leah DWORD ?
  25. BYTE 'Leah',0
  26.  
  27. .code
  28. _main:
  29. push OFFSET Itzhak
  30. push 0
  31. push OFFSET Linked
  32. call Insert
  33.  
  34. push OFFSET Sarah
  35. push OFFSET Abraham
  36. push OFFSET Linked
  37. call Insert
  38.  
  39. push OFFSET Leah
  40. push OFFSET Jacob
  41. push OFFSET linked
  42. call Insert
  43.  
  44. push OFFSET Linked
  45. call Min
  46.  
  47. call _ExitProcess@4
  48. ;
  49. ; Insert(HeadPtr, After, Item)
  50. ; Input parameters:
  51. ; HeadPtr Pointer to head (which points to first element)
  52. ; After Pointer to item before which we insert
  53. ; Item Pointer to item to insert
  54. ;
  55. Insert:
  56. HeadPtr = 8
  57. After = HeadPtr+4
  58. Item = After+4
  59. push ebp ;Standard prologue
  60. mov ebp,esp
  61.  
  62. push esi
  63. push edi
  64.  
  65. mov esi,After[ebp] ;After which element
  66. mov edi,Item[ebp] ;New element
  67.  
  68. cmp esi,0 ;To add as first?
  69. jne Normal ;No, usual handling
  70.  
  71. mov esi,HeadPtr[ebp];Yes, add 'after' the header
  72. Normal:
  73. mov eax,[esi]
  74. mov [edi],eax
  75.  
  76. mov [esi],edi
  77. done3:
  78. pop edi
  79. pop esi
  80.  
  81. pop ebp
  82.  
  83. ret 12
  84. ;
  85. ; Min(ptr)
  86. ; Input parameters:
  87. ; ptr Pointer to head (which points to first element)
  88. ;
  89. Min:
  90. HeadPtr = 8
  91. push ebp ;Standard prologue
  92. mov ebp,esp
  93.  
  94. push esi
  95. push ecx
  96. push ebx
  97.  
  98. mov ebx,0 ;Points to minimal string
  99. mov esi,HeadPtr[ebp]
  100. mov esi,[esi]
  101. cmp esi,0
  102. je EmptyList
  103.  
  104. mov ebx,esi
  105. add ebx,4 ;Set the first to be Minimal!
  106. NextElement:
  107. mov esi,[esi] ;Next element
  108. cmp esi,0 ;End of list?
  109. je Done ;Get out.
  110. mov ecx,esi ;Not the end. Need to check for minimality
  111. add ecx,4
  112. push ecx ;Current string
  113. push ebx ;Current minimal string
  114. call StrCmp ;Compare strings
  115. cmp eax,0
  116. jl NextElement ;Current minimal is not greater the current
  117. mov ebx,ecx ;Current minimal is greater than current.
  118. jmp NextElement ;
  119. Done:
  120. sub ebx,4
  121. Emptylist:
  122. mov eax,ebx ;Element with minimal string is returned
  123.  
  124. pop ebx
  125. pop ecx
  126. pop esi
  127.  
  128. pop ebp
  129. ret 4
  130.  
  131. ;
  132. ; int StrCmp(str1,str2)
  133. ; Input parameters:
  134. ; str1 Pointer to null terminated string
  135. ; str2 Pointer to null terminated string
  136. ; Return value:
  137. ; eax = -1 if str1 < str2
  138. ; eax = 0 if str1 == str2
  139. ; eax = 1 if str1 > str2
  140. StrCmp:
  141. str1=8
  142. str2=str1 + 4
  143. push ebp ;Standard prologue
  144. mov ebp,esp
  145.  
  146. push esi
  147. push edx
  148. push ebx
  149.  
  150. mov esi,str1[ebp] ;point to str1
  151. mov edi,str2[ebp] ;point to str2
  152. NextChar:
  153. mov al,[esi] ;Get character from str1
  154. mov bl,[edi] ;Get character from str2
  155. cmp al,bl ;Compare them
  156. jne Different ;If they are not equal, handle return value
  157. cmp al,0 ;They're equal. Are they null?
  158. je Same ;Yes. Then strings are equal
  159. inc esi ;No. So we need to check next characters
  160. inc edi
  161. jmp NextChar
  162. Same:
  163. mov eax,0 ;Strings equal are signaled with eax=0
  164. jmp done2
  165. Different:
  166. jg Greater
  167. mov eax,-1 ;str1 < str2 is signaled with eax=-1
  168. jmp done2
  169. Greater:
  170. mov eax,1 ;str1 > str2 is signaled with eax=1
  171. done2:
  172. pop ebx
  173. pop edx
  174. pop esi
  175.  
  176. pop ebp
  177.  
  178. ret 8
  179.  
  180. end _main

Report this snippet  

You need to login to post a comment.