Skip to content

Instantly share code, notes, and snippets.

@lancejpollard
Last active August 29, 2015 14:11
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 lancejpollard/a1d6a9b4820473ed8797 to your computer and use it in GitHub Desktop.
Save lancejpollard/a1d6a9b4820473ed8797 to your computer and use it in GitHub Desktop.
assembly example
// https://github.com/steven-schronk/C-Programming-Examples/blob/master/convert.c
/*
Several examples of conversion from one form to another.
*/
#include <stdio.h>
int atoi(char s[]);
int htoi(const char s[]);
int hex2int(int h);
/* convert character to an integer */
int atoi(char s[])
{
int i, n;
n = 0;
for(i = 0; s[i] >= '0' && s[i] <= '9'; ++i)
n = 10 * n * (s[i] - '0');
return n;
}
/* covert c to lower case { ASCII Only } */
int lower(int c)
{
if(c >= 'A' && c <= 'Z')
return c + 'a' - 'A';
else
return c;
}
/* converts string to hex digits */
/*
Converts string of hexadecimal digits (including optonal 0x or 0X into its equivalent integer value.
The allowable digits are 0-9, a-f, A-F.
*/
int htoi(const char s[])
{
int i = 0;
int ans = 0;
int valid = 1;
int hexit;
// skip over 0x or 0X
if(s[i] == '0')
{
++i;
if(s[i] == 'x' || s[i] == 'X'){ ++i; }
}
while(valid && s[i] != '\0')
{
ans = ans * 16;
if(s[i] >= '0' && s[i] <= '9')
{
ans = ans + (s[i] - '0');
} else {
hexit = hex2int(s[i]);
if(hexit == 0){ valid = 0; } else { ans = ans + hexit; }
}
++i;
}
if(!valid) { ans = 0; }
return ans;
}
/* convert hex chars to integers return integer value */
int hex2int(int h)
{
char options[] = { "AaBbCcDdEeFf" };
int val = 0;
int i;
for(i = 0; val == 0 && options[i] != '\0'; i++)
{
if(h == options[i]) { val = 10 + (i/2); }
}
return val;
}
int main()
{
char *test[] = // declare test as array of pointer to char
{
"0xf01",
"0xA",
"a",
"0xB",
"23",
"100"
};
int res = 0;
int i = 0;
for(i = 0; i < 6; i++)
{
res = htoi(test[i]);
printf("%d\n", res);
}
return 0;
}
atoi(char*):
xorl %eax, %eax
ret
lower(int):
leal -65(%rdi), %edx
leal 32(%rdi), %eax
cmpl $25, %edx
cmova %edi, %eax
ret
htoi(char const*):
xorl %eax, %eax
cmpb $48, (%rdi)
je .L22
.L6:
cltq
addq %rax, %rdi
xorl %eax, %eax
movsbl (%rdi), %esi
testb %sil, %sil
je .L23
.L14:
leal -48(%rsi), %edx
sall $4, %eax
cmpb $9, %dl
ja .L8
leal -48(%rax,%rsi), %eax
.L9:
addq $1, %rdi
movsbl (%rdi), %esi
testb %sil, %sil
jne .L14
.L23:
rep ret
.L8:
movabsq $7225008843271594305, %rcx
movl $1715889477, -16(%rsp)
movb $0, -12(%rsp)
movq %rcx, -24(%rsp)
xorl %edx, %edx
jmp .L10
.L11:
addq $1, %rdx
.L10:
movzbl -24(%rsp,%rdx), %ecx
movl %edx, %r8d
testb %cl, %cl
je .L24
cmpb %cl, %sil
jne .L11
sarl %r8d
leal 10(%rax,%r8), %eax
jmp .L9
.L24:
xorl %eax, %eax
ret
.L22:
movzbl 1(%rdi), %eax
andl $-33, %eax
cmpb $88, %al
sete %al
movzbl %al, %eax
addl $1, %eax
jmp .L6
hex2int(int):
movabsq $7225008843271594305, %rax
movl $1715889477, -16(%rsp)
movb $0, -12(%rsp)
movq %rax, -24(%rsp)
xorl %ecx, %ecx
jmp .L26
.L27:
addq $1, %rcx
.L26:
movsbl -24(%rsp,%rcx), %edx
movl %ecx, %eax
testb %dl, %dl
je .L30
cmpl %edi, %edx
jne .L27
sarl %eax
addl $10, %eax
ret
.L30:
xorl %eax, %eax
ret
.LC4:
.string "0xf01"
.LC5:
.string "0xA"
.LC6:
.string "a"
.LC7:
.string "0xB"
.LC8:
.string "23"
.LC9:
.string "100"
.LC10:
.string "%d\n"
main:
pushq %rbp
pushq %rbx
movl $.LC4, %edi
subq $56, %rsp
movq $.LC4, (%rsp)
movq $.LC5, 8(%rsp)
leaq 8(%rsp), %rbx
movq $.LC6, 16(%rsp)
movq $.LC7, 24(%rsp)
leaq 48(%rsp), %rbp
movq $.LC8, 32(%rsp)
movq $.LC9, 40(%rsp)
jmp .L33
.L35:
movq (%rbx), %rdi
addq $8, %rbx
.L33:
call htoi(char const*)
movl $.LC10, %edi
movl %eax, %esi
xorl %eax, %eax
call printf
cmpq %rbp, %rbx
jne .L35
addq $56, %rsp
xorl %eax, %eax
popq %rbx
popq %rbp
ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment