| 1 | = Asm code for finding first occurrence of letter in string = |
---|
| 2 | |
---|
| 3 | ''Part of'' TutorialIntermediate |
---|
| 4 | |
---|
| 5 | == Description == |
---|
| 6 | |
---|
| 7 | More or less equivalent to std.string.find. (also similar to C-RTL for strchr except finds count offset rather than char*). |
---|
| 8 | |
---|
| 9 | Demonstrates reference of local char[] variable, repne, scasb, etc. |
---|
| 10 | |
---|
| 11 | == Example == |
---|
| 12 | |
---|
| 13 | {{{ |
---|
| 14 | #!d |
---|
| 15 | /* |
---|
| 16 | * Copyright (C) 2004 by Digital Mars, www.digitalmars.com |
---|
| 17 | * Written by Lynn Allan |
---|
| 18 | * This software is provided 'as-is' by a rusty asm-486 programmer. |
---|
| 19 | * Released to public domain. |
---|
| 20 | * |
---|
| 21 | * Compile with: dmd test.d (uses dmd ver 2.046) |
---|
| 22 | */ |
---|
| 23 | |
---|
| 24 | import std.stdio; |
---|
| 25 | import std.string; |
---|
| 26 | |
---|
| 27 | void main() |
---|
| 28 | { |
---|
| 29 | string searchString = "The quick brown fox jumped over the lazy dog."; |
---|
| 30 | // 00000000001111111111222222222233333333334444 |
---|
| 31 | // 01234567890123456789012345678901234567890123 |
---|
| 32 | immutable(char)* pss = &searchString[0]; // searchString[0]; |
---|
| 33 | uint foundOffset = indexOf(searchString, 'z'); |
---|
| 34 | uint zCode = 'z'; |
---|
| 35 | uint len = searchString.length; |
---|
| 36 | writefln("z found at: %s", foundOffset); |
---|
| 37 | uint xCode = 'x'; |
---|
| 38 | foundOffset = indexOf(searchString, 'x'); |
---|
| 39 | len = searchString.length; |
---|
| 40 | writefln("x found at: %s", foundOffset); |
---|
| 41 | |
---|
| 42 | asm |
---|
| 43 | { |
---|
| 44 | cld; |
---|
| 45 | mov ECX,len; |
---|
| 46 | mov EAX,zCode; |
---|
| 47 | mov EDI,pss; |
---|
| 48 | repne; |
---|
| 49 | scasb; |
---|
| 50 | mov EBX,len; |
---|
| 51 | sub EBX,ECX; |
---|
| 52 | dec EBX; |
---|
| 53 | mov foundOffset,EBX; |
---|
| 54 | } |
---|
| 55 | writefln("z found at: %s", foundOffset); |
---|
| 56 | assert(foundOffset == 38); |
---|
| 57 | |
---|
| 58 | asm |
---|
| 59 | { |
---|
| 60 | cld; |
---|
| 61 | mov ECX,len; |
---|
| 62 | mov EAX,xCode; |
---|
| 63 | mov EDI,pss; |
---|
| 64 | repne; |
---|
| 65 | scasb; |
---|
| 66 | mov EBX,len; |
---|
| 67 | sub EBX,ECX; |
---|
| 68 | dec EBX; |
---|
| 69 | mov foundOffset,EBX; |
---|
| 70 | } |
---|
| 71 | writefln("x found at: %s", foundOffset); |
---|
| 72 | assert(foundOffset == 18); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | |
---|
| 76 | }}} |