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 [email protected]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. ;
  48. ; Insert(HeadPtr, After, Item)
  49. ; Input parameters:
  50. ; HeadPtr Pointer to head (which points to first element)
  51. ; After Pointer to item before which we insert
  52. ; Item Pointer to item to insert
  53. ;
  54. Insert:
  55. HeadPtr = 8
  56. After = HeadPtr+4
  57. Item = After+4
  58. push ebp ;Standard prologue
  59. mov ebp,esp
  60.  
  61. push esi
  62. push edi
  63.  
  64. mov esi,After[ebp] ;After which element
  65. mov edi,Item[ebp] ;New element
  66.  
  67. cmp esi,0 ;To add as first?
  68. jne Normal ;No, usual handling
  69.  
  70. mov esi,HeadPtr[ebp];Yes, add 'after' the header
  71. Normal:
  72. mov eax,[esi]
  73. mov [edi],eax
  74.  
  75. mov [esi],edi
  76. done3:
  77. pop edi
  78. pop esi
  79.  
  80. pop ebp
  81.  
  82. ret 12
  83. ;
  84. ; Min(ptr)
  85. ; Input parameters:
  86. ; ptr Pointer to head (which points to first element)
  87. ;
  88. Min:
  89. HeadPtr = 8
  90. push ebp ;Standard prologue
  91. mov ebp,esp
  92.  
  93. push esi
  94. push ecx
  95. push ebx
  96.  
  97. mov ebx,0 ;Points to minimal string
  98. mov esi,HeadPtr[ebp]
  99. mov esi,[esi]
  100. cmp esi,0
  101. je EmptyList
  102.  
  103. mov ebx,esi
  104. add ebx,4 ;Set the first to be Minimal!
  105. NextElement:
  106. mov esi,[esi] ;Next element
  107. cmp esi,0 ;End of list?
  108. je Done ;Get out.
  109. mov ecx,esi ;Not the end. Need to check for minimality
  110. add ecx,4
  111. push ecx ;Current string
  112. push ebx ;Current minimal string
  113. call StrCmp ;Compare strings
  114. cmp eax,0
  115. jl NextElement ;Current minimal is not greater the current
  116. mov ebx,ecx ;Current minimal is greater than current.
  117. jmp NextElement ;
  118. Done:
  119. sub ebx,4
  120. Emptylist:
  121. mov eax,ebx ;Element with minimal string is returned
  122.  
  123. pop ebx
  124. pop ecx
  125. pop esi
  126.  
  127. pop ebp
  128. ret 4
  129.  
  130. ;
  131. ; int StrCmp(str1,str2)
  132. ; Input parameters:
  133. ; str1 Pointer to null terminated string
  134. ; str2 Pointer to null terminated string
  135. ; Return value:
  136. ; eax = -1 if str1 < str2
  137. ; eax = 0 if str1 == str2
  138. ; eax = 1 if str1 > str2
  139. StrCmp:
  140. str1=8
  141. str2=str1 + 4
  142. push ebp ;Standard prologue
  143. mov ebp,esp
  144.  
  145. push esi
  146. push edx
  147. push ebx
  148.  
  149. mov esi,str1[ebp] ;point to str1
  150. mov edi,str2[ebp] ;point to str2
  151. NextChar:
  152. mov al,[esi] ;Get character from str1
  153. mov bl,[edi] ;Get character from str2
  154. cmp al,bl ;Compare them
  155. jne Different ;If they are not equal, handle return value
  156. cmp al,0 ;They're equal. Are they null?
  157. je Same ;Yes. Then strings are equal
  158. inc esi ;No. So we need to check next characters
  159. inc edi
  160. jmp NextChar
  161. Same:
  162. mov eax,0 ;Strings equal are signaled with eax=0
  163. jmp done2
  164. Different:
  165. jg Greater
  166. mov eax,-1 ;str1 < str2 is signaled with eax=-1
  167. jmp done2
  168. Greater:
  169. mov eax,1 ;str1 > str2 is signaled with eax=1
  170. done2:
  171. pop ebx
  172. pop edx
  173. pop esi
  174.  
  175. pop ebp
  176.  
  177. ret 8
  178.  
  179. end _main

Report this snippet  

You need to login to post a comment.