Guest User

Untitled

a guest
Apr 16th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.                                                                      
  2.                                                                      
  3.  ;Chris Ruskai                                                                    
  4.                                              
  5. TITLE CharacterSearch.asm
  6.  
  7.  
  8.  
  9. INCLUDE Irvine32.inc
  10.  
  11.  
  12.  
  13. PrepareMap PROTO,
  14.     string: PTR BYTE,
  15.     map: PTR BYTE
  16.  
  17.  
  18.  
  19. CharacterSearch PROTO,
  20.         map: PTR BYTE,
  21.         char: BYTE
  22.  
  23. .data
  24. stringMap byte 32 dup(0)
  25. testString byte "hello, assembly language programming!",0
  26. msg1 byte " is not in the string.",0
  27. msg2 byte " is in the string.",0
  28.  
  29. ;Code test driver
  30. .code
  31.     main PROC
  32.  
  33.  
  34.    
  35.            
  36.  
  37.     INVOKE PrepareMap, addr testString, addr stringMap
  38.    
  39.     ;Generate Random Characters
  40.     call Randomize
  41.     mov esi, 0
  42.     L1:
  43.     cmp esi, 9
  44.     JG L2
  45.     mov eax, 25
  46.     call RandomRange
  47.     add eax, 97d
  48.     call WriteChar
  49.     INVOKE CharacterSearch, addr stringMap, al
  50.     inc esi
  51.     JMP L1
  52.  
  53.     L2:
  54.        
  55.  
  56.     exit
  57.     main ENDP
  58.    
  59.  
  60.  
  61.     ;PrepareMap Procedure. Takes the test string and the map as parameters.
  62.     PrepareMap PROC USES ECX EBX EAX EDX ESI EDI EBP, string: PTR BYTE, map: PTR BYTE
  63.    
  64.    
  65.     ;mov string offsets and set counter
  66.     mov esi, string
  67.     mov edi, map
  68.     mov edx, map
  69.     mov ebp, 0
  70.  
  71.  
  72. ;for(int i = 0; i < strlength; i++)
  73. ForLoop:
  74.    
  75.     ;length of string
  76.     cmp ebp, 32
  77.     ;end loop if i >= 32
  78.     JGE L9
  79.  
  80.     ;map[(string[i])>>3] |= 1<<( (string[i]) &7)
  81.    
  82.     ;map[(string[i]>>3]
  83.     LeftSide:
  84.        
  85.         mov al, [esi]
  86.         movzx eax, al
  87.         shr eax, 3
  88.         add edi, eax
  89.  
  90.  
  91.     ;1 << ((string[i]) &7)
  92.     RightSide:
  93.         mov cl, [esi]
  94.         AND cl, 7
  95.         mov bl, 1
  96.         shl bl, cl
  97.  
  98.     ; LefSide |= RightSide
  99.     OrStatement:
  100.         OR [edi], bl
  101.         inc ebp
  102.         inc esi
  103.         mov edi, edx
  104.         add edi, 1
  105.         inc edx
  106.  
  107.        
  108.        
  109.        
  110.        
  111.         ;Restart loop
  112.         JMP ForLoop
  113.  
  114.  
  115.    
  116.     ;End Loop
  117.     L9:
  118.         ret
  119.     PrepareMap Endp
  120.  
  121.  
  122.  
  123.  
  124.     CharacterSearch PROC map: PTR BYTE, char: BYTE
  125.  
  126.     ;map[ch >> 3]
  127.     LeftSide:
  128.         ;mov al, char
  129.         ;call WriteChar
  130.         mov edi, map
  131.         mov cl, char
  132.         shr cl, 3
  133.         add [edi], cl
  134.    
  135.     ;(1 << ((string[i]) & 7)
  136.     RightSide:
  137.         mov cl, char
  138.         AND cl, 7
  139.         mov al, 1
  140.         shl al, cl
  141.  
  142.     ;LeftSide & RightSide
  143.     AndStatement:
  144.         mov bl, [edi]
  145.         AND bl, al
  146.  
  147.     ;If AndStatement != 0, character is here.
  148.     IfStatement:
  149.         cmp bl, 0
  150.         JE NotIn
  151.         mov edx, offset msg2
  152.         call WriteString
  153.         call crlf
  154.         JMP L9
  155.  
  156.     ;Else, not in string.
  157.     NotIn:
  158.         mov edx, offset msg1
  159.         call WriteString
  160.         call crlf
  161.  
  162.     ;end proc
  163.     L9:
  164.     ret
  165.     CharacterSearch endp
  166.    
  167.    
  168.     END main
Add Comment
Please, Sign In to add comment