Advertisement
Guest User

Untitled

a guest
Jun 7th, 2018
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; filename: strings.nasm
  2. ; author: lawly
  3. ; description: example of string instructions
  4. ;
  5.  
  6. global _start
  7.  
  8. section .text
  9.  
  10. PrintDestination:
  11.     mov eax, 0x4
  12.     mov ebx, 0x1
  13.     mov ecx, destination
  14.     mov edx, sourceLen
  15.     int 0x80
  16.     ret
  17.  
  18. PrintString:
  19.     mov eax, 0x4
  20.     mov ebx, 0x1
  21.     int 0x80
  22.     ret
  23.  
  24. _start:    
  25.     ; copy string from source to destination
  26.     mov ecx, sourceLen              ; load message length into ECX
  27.     lea esi, [source]               ; load effective address of source into ESI
  28.     lea edi, [destination]          ; load effective address of source into EDI
  29.  
  30.     cld                             ; clear direction flag
  31.     rep movsb                       ; repeat while ECX != 0, copies 1 byte from source to dest
  32.                                     ; DF = 1, copy from high to low memory
  33.                                     ; increments ESI/EDI ECX-times!
  34.  
  35.     ; string comparison with cmpsb
  36.     ; compare source and destination
  37.     mov ecx, sourceLen
  38.     lea esi, [source]
  39.     lea edi, [comparison]          
  40.     repe cmpsb                      ; repeat while equal, terminates when: ECX = 0 / ZF = 0
  41.                                     ; "Hello Word" = "Hello"?
  42.  
  43.     jz isEqual                     ; jump if strings were equal
  44.     mov ecx, strUnequal
  45.     mov edx, strUnequalLen
  46.     call PrintString              
  47.     jmp exit
  48.  
  49. isEqual:
  50.     mov ecx, strEqual
  51.     mov edx, strEqualLen
  52.     call PrintString
  53.  
  54. exit:
  55.     ; exit
  56.     mov eax, 0x1
  57.     mov ebx, 0x2
  58.     int 0x80
  59.  
  60. section .data
  61.     source:         db      "Hello Word", 0xa
  62.     sourceLen:      equ     $-source
  63.  
  64.     comparison:     db      "Hello"
  65.  
  66.     strEqual:       db      "Strings are equal", 0xa
  67.     strEqualLen:    equ     $-strEqual
  68.  
  69.     strUnequal:     db      "Strings are not equal", 0xa
  70.     strUnequalLen:  equ     $-strUnequal
  71.  
  72. ; section for unititialized data
  73. section .bss
  74.     destination:    resb    100
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement