Log in

View Full Version : Need help with very simple ASM program


mail1459508
March 30th, 2007, 21:30
I am using asm and ld on a Linux box. The program works fine as long
as the "mov [ecx + 8], dl" is commented out. I'm just trying to change
the "X" to a "Y".

But when I uncomment that line out, the program has a segmentation
fault. I can't figure out what the problem is.

Could somebody please clue me in?

Thank you.


global _start

_start:
xor eax, eax
xor ebx, ebx
xor ecx, ecx
xor edx, edx
jmp short string

code:
pop ecx
mov edx, 89
;mov [ecx + 8], dl
xor edx, edx
mov dl, 10
mov bl,1
mov al,4
int 0x080
dec bl
mov al,1
int 0x80

string:
call code
db 'Testing X', 0x0A

OHPen
March 31st, 2007, 00:04
Hi,

im not an expert concerning assembler in linux systems, but maybe the problem is that you only try to move 1 byte into a pointer which is normal expected to receive a dword.
Try something like that:

instead of

mov [ecx + 8], dl

try

mov byte ptr [ecx + 8], dl

dunno if this syntax is known to your compiler, but you can give it a try

Hope this will help you,

PAPiLLiON

blabberer
March 31st, 2007, 01:28
naides call pushes a retn address which happens to be a db X
so pop ecx will get the retn address which would be in esp

now as to why it segfaults i believe he is writing to non writable code section

no linux problem here afaik


edit
yes that is the cause

Code:

:~/mailcrash> nasm -f elf mailcrash.s && ld -N -o mailcrash mailcrash.o && ./mailcrash && nasm -f elf mailcrash.s && ld -o mailcrash mailcrash.o && ./mailcrash
Testing Y
Segmentation fault
:~/mailcrash>

naides
March 31st, 2007, 03:58
@Blabberer:

I realized what you explained and decided to delete my posting, because it was confused and confusing, before I saw that you had read it and referred to it in your answer.

Sorry.

Now, is this a standard way to code in Linux?

me thinks using the implicit "push return address" associated with the call instruction as a method to pass a pointer address to a string, well.... kinky at best,
rather unnecessarily murky and confusing.

LLXX
March 31st, 2007, 05:39
Here's what I would've written (I assume you're writing an RCF?)
Code:
_start:
xor eax, eax
xor ebx, ebx
xor ecx, ecx
call $+5
ekp:
pop ecx
mov [ecx + string-ekp+8], y89
xor edx, edx
mov dl, 10
mov bl,1
mov al,4
int 128
dec ebx
mov al,1
int 128
string:
db 'Testing X', 10
Might've gotten the syntax wrong but that's just because I'm used to a different syntax.

blabberer
March 31st, 2007, 10:56
@naides

no this is not how you code on *nix only

thats how many start thier coding career via google and that is the most easiest way to code without having to declare everyting upfront and be formal

when you shift to compilers especially those that are more lenient with forward referancing you normally declare things as and when you feel the need (the preprocessors knows them to shift them to data sections but analogically you simply code like what is shown above)

iirc if you look around in win32asm you will find several big 20 page locked topics where people blow fire and brimstone on each others for using strings in .code section ( there are several white papers that enumerate performance hit/extra performance on kinky codes in latest processors when data and code is mixed in sections look for prefetch stall and other big technical words )

LLXX
March 31st, 2007, 16:59
Quote:
[Originally Posted by blabberer;64731]iirc if you look around in win32asm you will find several big 20 page locked topics where people blow fire and brimstone on each others for using strings in .code section ( there are several white papers that enumerate performance hit/extra performance on kinky codes in latest processors when data and code is mixed in sections look for prefetch stall and other big technical words )
Lol. I usually put everything in one section, but I don't mix code and data; usually the sequence is code, data, then uninitialised data all in one section. But whatever way you write it, the Asm program is always going to be more efficient than equivalent in HLL. Also, any decent assembler (and MASM) supports forward references, so I'd say the applications of interleaved code and data are rather limited.