This commit is contained in:
Ingy döt Net 2013-06-05 21:47:54 +00:00
parent 1f1ad49427
commit 6f050a029e
2496 changed files with 37609 additions and 3031 deletions

View file

@ -0,0 +1,23 @@
text
reverse(text s)
{
data b;
integer i;
i = length(s);
while (i) {
i -= 1;
b_insert(b, -1, character(s, i));
}
return b_string(b);
}
integer
main(void)
{
o_text(reverse("Hello, World!"));
o_byte('\n');
return 0;
}

View file

@ -1,8 +1,8 @@
Function StrRev1(ByVal $p1)
dim $b = ""
repeat len(p1)
b = b & right(p1,1)
p1 = left(p1,len(p1)-1)
end repeat
REPEAT len(p1)
b = b & RIGHT(p1,1)
p1 = LEFT(p1,LEN(p1)-1)
END REPEAT
return b
End Function

View file

@ -1,8 +1,7 @@
Function StrRev2(ByVal $p1)
dim $b = ""
dim %i
for i = len(p1) downto 1
b = b & mid(p1,i,1)
dim $b = "", %i
for i = len(p1) DOWNTO 1
b = b & MID(p1,i,1)
next
return b
End Function

View file

@ -0,0 +1,49 @@
DYNASM RevStr(BYVAL s AS STRING) AS STRING
// get length of string
// divide by two
// setup pointers to head and tail
// iterate from 1 to (length \ 2)
// swap head with tail
// increment head pointer
// decrement tail pointer
ENTER 0, 0 // = PUSH EBP: MOV EBP, ESP
PUSH EBX // by Windows convention EBX, EDI, ESI must be saved before modification
MOV EAX, s // get string pointer
MOV ECX, EAX // duplicate it
.WHILE BYTE PTR [ECX] <> 0
INC ECX // propagate to tail
.WEND
MOV EDX, ECX // duplicate tail pointer
DEC EDX // set it to last byte before trailing zero
SUB ECX, EAX // get length in ECX in 1 CPU cycle
SHR ECX, 1 // get length \ 2 in 1 CPU cycle; that's the beauty of power-of-two division
.WHILE ECX > 0
MOV BL, [EDX] // no need to XOR; just overwrite BL and BH contents
MOV BH, [EAX] // DynAsm deduces data size from destination register sizes
MOV [EDX], BH // ditto, source register sizes
MOV [EAX], BL
INC EAX // propagate pointers
DEC EDX
DEC ECX // decrement counter
.WEND
// point to start of string again
MOV EAX, s // MOV = 1 CPU cycle, PUSH + POP = 2 CPU cycles
POP EBX // by Windows convention ESI, EDI, EBX must be restored if modified
LEAVE // = POP EBP
RET
END DYNASM

View file

@ -0,0 +1 @@
"asdf" reverse

View file

@ -0,0 +1,6 @@
#!/bin/bash
str=abcde
for((i=${#str}-1;i>=0;i--)); do rev="$rev${str:$i:1}"; done
echo $rev