Skip to content

Instantly share code, notes, and snippets.

@jido
Last active March 17, 2019 19:02
Show Gist options
  • Save jido/0207a81356742978f50d32f0feb492f3 to your computer and use it in GitHub Desktop.
Save jido/0207a81356742978f50d32f0feb492f3 to your computer and use it in GitHub Desktop.
.intel_syntax noprefix
_add:
mov eax, edi
add eax, esi
jo plus_overflow
jmp r8
plus_overflow:
jmp r9
_null:
cmp edi, 0
jne null_is_not
jmp r8
null_is_not:
jmp r9
_times:
mov eax, edi
imul eax, esi
jo times_overflow
jmp r8
times_overflow:
jmp r9
## toPower algorithm:
## 0 ** n = 0
## 1 ** n = 1
## x ** 0 = 1
## decompose the exponent n in powers of two
## for each factor k, x ** n = product( x ** k )
## so we can square x until we get the answer
## example: 3 ** 5 = (3 ** 1) * (3 ** 4)
## = 3 * ((3 * 3) * (3 * 3))
## = 243
.p2align 4
_toPower:
mov eax, 0
cmp edi, 0
je toPower_success ## shortcut if arg1 = 0
mov eax, 1
cmp edi, 1
je toPower_success ## shortcut if arg1 = 1
cmp esi, 0
je toPower_success ## shortcut if arg2 = 0
jl toPower_failed ## cannot do negative numbers
push r8 ## save return continuation
push r9 ## save event continuation
mov eax, 1
push rax ## calculation results
lea r9, [rip + toPower_overflow]
mov ecx, esi
toPower_loop:
test ecx, 1
jz toPower_next ## if exponent bit not set, don't update result
pop rsi
lea r8, [rip + toPower1]
jmp _times ## note: assumes ecx is preserved
toPower1:
push rax
toPower_next:
shr ecx, 1
jz toPower_done
mov esi, edi
lea r8, [rip + toPower2]
jmp _times ## note: assumes ecx is preserved
toPower2:
mov edi, eax ## take square of edi
jmp toPower_loop
toPower_done:
pop rax
pop r9
pop r8
toPower_success:
jmp r8
toPower_overflow:
pop rax
pop r9
pop r8
toPower_failed:
jmp r9
.globl _main ## -- Begin function main
.p2align 4
_main: ## @main
mov edi, 3
mov esi, 56
lea r8, [rip + next]
lea r9, [rip + zero]
jmp _toPower
zero:
mov eax, -1
next:
ret
## -- End function
.data
.globl _int32
_int32:
.long 0
outofmemory_message:
.asciz "ERROR: Out of memory"
(lldb)
Process 24797 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = instruction step over
frame #0: 0x0000000100000f4a int32`toPower_loop + 12
int32`toPower_loop:
-> 0x100000f4a <+12>: popq %rsi
0x100000f4b <+13>: leaq 0x5(%rip), %r8 ; toPower1
0x100000f52 <+20>: jmp 0x100000eef ; times
int32`toPower1:
0x100000f57 <+0>: pushq %rax
Target 0: (int32) stopped.
(lldb)
Process 24797 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = instruction step over
frame #0: 0x0000000100000f4b int32`toPower_loop + 13
int32`toPower_loop:
-> 0x100000f4b <+13>: leaq 0x5(%rip), %r8 ; toPower1
0x100000f52 <+20>: jmp 0x100000eef ; times
int32`toPower1:
0x100000f57 <+0>: pushq %rax
int32`toPower_next:
0x100000f58 <+0>: shrl %ecx
Target 0: (int32) stopped.
(lldb)
Process 24797 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = instruction step over
frame #0: 0x0000000100000f52 int32`toPower_loop + 20
int32`toPower_loop:
-> 0x100000f52 <+20>: jmp 0x100000eef ; times
int32`toPower1:
0x100000f57 <+0>: pushq %rax
int32`toPower_next:
0x100000f58 <+0>: shrl %ecx
0x100000f5a <+2>: je 0x100000f75 ; toPower_done
Target 0: (int32) stopped.
(lldb)
Process 24797 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
frame #0: 0x0000000000000000
error: memory read failed for 0x0
Target 0: (int32) stopped.
(lldb)
(lldb) n
Process 24894 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = instruction step over
frame #0: 0x0000000100000ef1 int32`times + 2
int32`times:
-> 0x100000ef1 <+2>: imull %esi, %eax
0x100000ef4 <+5>: jo 0x100000efd ; times_overflow
0x100000efa <+11>: jmpq *%r8
int32`times_overflow:
0x100000efd <+0>: jmpq *%r9
Target 0: (int32) stopped.
(lldb) reg read r8 r9 edi esi
r8 = 0x0000000100000f57 int32`toPower1
r9 = 0x0000000100000f7d int32`toPower_overflow
edi = 0x0290d741
esi = 0x000019a1
(lldb) n
Process 24894 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = instruction step over
frame #0: 0x0000000100000ef4 int32`times + 5
int32`times:
-> 0x100000ef4 <+5>: jo 0x100000efd ; times_overflow
0x100000efa <+11>: jmpq *%r8
int32`times_overflow:
0x100000efd <+0>: jmpq *%r9
int32`toPower:
0x100000f00 <+0>: movl $0x0, %eax
Target 0: (int32) stopped.
(lldb) reg read r8 r9 edi esi
r8 = 0x0000000100000f57 int32`toPower1
r9 = 0x0000000100000f7d int32`toPower_overflow
edi = 0x0290d741
esi = 0x000019a1
(lldb) n
Process 24894 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
frame #0: 0x0000000000000000
error: memory read failed for 0x0
Target 0: (int32) stopped.
(lldb)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment