Skip to content

Instantly share code, notes, and snippets.

@StefanoBelli
Created September 17, 2018 02:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StefanoBelli/e85d5d788aea8dca758ce6b9dccafe29 to your computer and use it in GitHub Desktop.
Save StefanoBelli/e85d5d788aea8dca758ce6b9dccafe29 to your computer and use it in GitHub Desktop.
twitch stream
//I don't like intel syntax..
//lemme see
#include <cstdint>
//i like stdcall...
//shit I am wasted
// I like using stdcall, so we used that calling convention
// (we don't have to worry about cleaning the stack and we just push arguments on the stack
// Returns: base ^ exp
std::uint32_t __stdcall pow_unsigned(std::uint32_t base, std::uint32_t exp) {
int res;
__asm {
mov eax, 1
mov ecx, [exp]
mov ebx, [base]
test ecx, ecx
jz _finish
_pow_loop0_lol:
mul ebx
test ecx, ecx
jz _finish
sub ecx, 1
jnz _pow_loop0_lol
_finish:
mov [res], eax
}
return res;
}
//my lower back is crying
//lets use size_t to get digits length
//but i don't think we will get a digit of that size ever...
//to get a digit we think of doing considering digit position
//
// (1234)dec = 1 * 1000 + 2 * 100 + 3 * 10 + 4
//lets do this in assembly...
//this finally works
//
// Returns: the unsigned integer from buf
// buf: buffer containing valid decimal digits
// len: string length (how many digits are in there)
// endptr: if is not NULL, this means that there was a non-valid character (non decimal digit)
// does not perform check on overflow and underflow
// correct conversion goes from 0 to INT_MAX (or std::numeric_limits<int>::max() )
std::uint32_t stoui(char* buf, std::size_t len, char** endptr) {
std::uint32_t result; //store the result here
__asm {
xor esi, esi //result
mov eax, 1 //positional result (this was an issue)
mov ebx, buf //beginning of the buffer [memory address]
xor ecx, ecx //counter
mov edx, [len] //length (number of digits)
_stoui_loop0: //we'll jump back here if condition is not met
push edx
mov dl, [ebx]
cmp dl, 48 //check if character is a valid decimal digit (0-9)
jl _failure
cmp dl, 57
jg _failure
pop edx
push edx
push ecx
mov edi, edx //edi = size
sub edi, ecx //size - pos
sub edi, 1 //edi - 1
push edi
push 10
call pow_unsigned
pop ecx
xor edx, edx
mov dl, [ebx]
sub dl, 48 //char - 48
mul edx //ebx * eax
pop edx
add esi, eax
add ebx, 1 //increment the character pointer
add ecx, 1 //increment the counter
cmp ecx, edx
jne _stoui_loop0 //if counter != len then come back (loop again)
je _success //i kinda hate this solution... but for now it is ok
_failure:
pop edx
mov edx, endptr
mov [edx], ebx
_success:
mov [result], esi
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment