View Full Version : x86 asm fun
Strike
05-28-2002, 06:52 PM
Okay, I'm working on the hex dumper for the "as many languages..." thread, but it's segfaulting on me. I'm not done debugging it yet, but I wanted to see if anyone had any "you moron, don't do that" remarks to say about this:
section .text
global _start
filename db "hexdump.asm"
mode db "r"
format db "%X "
buf db 1
count dw 1
fd dw 0
_start:
; Save registers that we will use
push esp
push eax
push ebx
push ecx
push edx
; Open file, put file descriptor in fd
mov ebx, [filename] ; put filename into ebx as first arg of open()
mov ecx, 0 ; no flags for open()
mov edx, [mode] ; read-only mode for open()
mov eax, 5 ; 5 = open()
int 0x80
mov [fd], eax ; file descriptor from eax to fd
; Read bytes from file, printing out each in hex
stringloop:
xor eax, eax ; zero out eax
cmp [count], eax ; see if the buffer has 0 stored in it
je end ; if it is, end
mov ebx, [fd] ; first arg to read() - fd
mov ecx, [buf] ; second arg = buf
mov edx, 1 ; 1 byte
mov eax, 3 ; read() = 3
int 0x80
mov [count], eax ; bytes read go into count
; Now we need to call write to stdout
mov ebx, 1 ; first arg - fd = 1 (stdout)
mov ecx, [buf] ; second arg - buffer
mov edx, 1 ; third arg - count
mov eax, 4 ; write() = 4
int 0x80
jmp stringloop
end:
; restore registers
pop edx
pop ecx
pop ebx
pop eax
pop esp
ret
Note, this is using Linux syscalls.
Strike
05-28-2002, 06:58 PM
Of course, this is only supposed to essentially "cat" the text file (no hex conversion), but even that doesn't work
file13
05-29-2002, 01:11 PM
i'll try to screw with it. but have you considered importing printf from C and calling it a day?
Strike
05-29-2002, 01:35 PM
Well, my information on using libc calls isn't very good. I'm not sure if it uses the same registers for arguments as syscalls or not. If you could tell me that, I'd give it a shot.
file13
05-29-2002, 03:01 PM
i can't remember the exact syntax myself. i have a book at home that shows how to do it. if you got time now though you could ask over here:
http://www.octium.net/nasm/forum/
Frank Kotler (from comp.lan.asm.x86) is there all the time and he's VERY helpful.
my asm sounds as good as your...kinda sorta with lots of stenious effort. :) but probably using like getc and printf if would make it much easier. but who knows. i'm not very good with the syscalls either.
good luck and if wrose case scenario, do what hackers do best. keep hacking with it till it works! :D
Strike
05-29-2002, 04:17 PM
Hrm, I hadn't even thought to search Google Groups for that kind of info.... I'll do that first and then check out your link. Thanks :D I really didn't think anyone would respond to this thread whatsoever...
file13
05-29-2002, 05:09 PM
np. :)
i was considering doing it myself, but luckly someone else stepped up to the plate....whew! ;)
good luck and if not i'll check on that tonight. i know it basically where you export the calls and i think you just do it BSD style by putting in all the args on the stack, then useing CALL to invoke the function. then just link it with gcc. but can't remember the specifics....DOH!
Strike
05-29-2002, 05:28 PM
Yeah, it is simply a matter of pushing the args in reverse order and then calling the function (return goes in eax). I actually have that "cat"-like program working fully now:
global main
extern printf
extern putchar
extern fopen
extern fread
[section .text]
main:
push ebp ; set up stack frame for debugger
mov ebp, esp
; Open file
mov eax, fileMode ; Last arg - mode
push eax
mov eax, fileName ; First arg - filename
push eax
call fopen
mov [filePtr], eax ; store FILE* in filePtr
add esp, 8
; Start read loop
mov eax, 1
readloop:
cmp eax, 0
je end
mov eax, [filePtr]
push eax ; last arg - FILE*
mov eax, 1 ; next 2 - size of chunk, # of chunks
push eax ; both are 1
push eax ; we just push it twice
mov eax, buf
push eax ; first arg = buffer (char *)
call fread
add esp, 16 ; clear the stack
push eax ; save eax for later
mov eax, buf
push eax ; push buf back on for printf
push dword formatStr ; and format string
call printf
add esp, 8 ; get those last 2 args off stack
pop eax ; put last thing on stack back into eax
jmp readloop
end:
mov eax, 10 ; Flush buffer with newline and putchar
push eax
call putchar
add esp, 4
mov esp, ebp ; Destroy stack frame
pop ebp
ret
[section .data]
formatHex db "%X ", 0
formatDec db "%d ", 0
formatStr db "%s", 0
filePtr db 0
fileName db "hexdump.asm", 0
fileMode db "r", 0
buf db "a", 0
Some of the extra vars in the .data section are from test case stuff or future plans. But that works fine, just:
nasm -f elf hexdump.asm
gcc -o hexdump hexdump.o
./hexdump
And it should cat itself (granted that you named it hexdump.asm).
[/code]
Strike
05-29-2002, 05:37 PM
Woohoo! Two simple switches make this the hex dumper:
Change:
push eax ; save eax for later
mov eax, buf
push eax ; push buf back on for printf
push dword formatStr ; and format string
to
push eax ; save eax for later
mov eax, [buf]
push eax ; push buf back on for printf
push dword formatHex ; and format string
So that we are passing the actual value of buf and not the pointer since we will now be using %X which expects an int of some sort and not a char *, and so that we actually use the "%X " format string instead of the "%s" format string.
file13
05-29-2002, 05:46 PM
SWEEEEEEEEEEEEEEEEEEEEEEET! :D
good work man!
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.