Skip to content

Instantly share code, notes, and snippets.

@samsonjs
Created January 7, 2010 04:42
Show Gist options
  • Save samsonjs/270996 to your computer and use it in GitHub Desktop.
Save samsonjs/270996 to your computer and use it in GitHub Desktop.
asm.block do
# low nybble of nth byte
movzx(EBX, AL)
and_(BL, 0x0f) # isolate low nybble
movzx(EDX, [:byte, ESI+EBX])
mov([EDI], DL)
dec(EDI)
# high nybble of nth byte
movzx(EBX, AL)
and_(BL, 0xf0) # isolate high nybble
shr(BL, 4)
mov(DL, [ESI+EBX])
mov([EDI], DL)
dec(EDI)
shr(EAX, 8)
loop_(loop_label)
# write(int fd, char *s, int n)
mov(EAX, 4) # SYS_write
lea(ECX, [var(h)]) # ecx = &s
args = [1, # fd = 1 (STDOUT)
ECX, # s = &s
11] # n = 11 (excluding term, max # of chars to print)
case platform
when 'darwin' # on the stack, right to left (right @ highest addr)
####
# setup bogus stack frame
push(EBP)
mov(EBP, ESP)
sub(ESP, 36)
####
args.reverse.each { |a| push(a) }
push(EAX)
int(0x80)
####
# teardown bogus stack frame
xor(EAX, EAX)
add(ESP, 36)
pop(EBX)
leave
####
when 'linux'
mov(EBX, args[0])
mov(ECX, args[1])
mov(EDX, args[2])
int(0x80)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment