• Notice regarding image hosts and attachments. See here.

ASM refresher

Associate
Joined
22 Sep 2009
Posts
2,085
Location
Leicester
I've started relearning ASM, I didn't realise just how much I'd forgotten over the years :p

Anyway, I'm using NASM and we'll assume x86 Linux for now, I remember some bits and pieces and I've managed to figure a few things out reading example code online, but there are a few things which I'm slightly confused about and was wondering if anyone could elaborate on what things were doing (if you can explain it in C terms that's even better)! My experience comes mainly from programming micro controllers so obviously there are bits and pieces which I've not needed in the past.

My first issue is working with variable strings, I can't remember if this is possible but typing all my strings in the data segment is getting tedious, can I move a string of letters into a resb 256 for example?

Basically this into ASM:
Code:
strcpy(string, "Hello World!");
for example.

Second is modifying individual bits, how would one go about it?

I'm aware I could probably figure these out using a disassembler, but would rather ask here.

There is more, however I'm hoping that I'll be able to figure that out myself I've got these 2 bases covered, if not I'll be posting back ;)
 
@NickK I did say x86 Linux, I thought it'd be pretty obviously Intel x86 Linux :rolleyes:

@tntcoder Thanks for that, I'll give it a shot. I vaguely remember doing a tad of ASM (for Windows) and using macros to copy strings, so now I'll look into doing that again.

Ok, written a macro following the NASM documentation (fantastic source of info).

Code:
%macro	STRING 1+
	jmp %%endstr
%%str:	db	%1
%%endstr:
	mov	edx,%%str					; move string address to dx
	mov	ecx,11						; length
	mov	ebx,1
	mov	eax,4						; sys_write
	int	0x20						; call kernel
%endmacro

segment .data

segment .bss

segment .text

	[GLOBAL str_asm]
	
	str_asm:
		
;		STRING "Hello World"
		
		mov	ebx,0					; exit number
		mov	eax,1					; sys_exit
		int	0x20					; call kernel

Now I assemble this, run it and segfault, even removing the macro completely has no effect. Assembles fine just won't run, strace has given me nothing useful to go on either.
 
Last edited:
EDIT:
Ignore the below post, changing 0x20 to 0x80 worked, I have no clue how I got myself confused, my previous code to create files called 0x80 so I can't figure out where 0x20 came from...

Well if you notice I commented out that and as I say, I removed the macro completely and it still segfaults.

@matja I hadn't noticed the edx/ecx mixup so thanks for pointing that out. Trying to figure out where I plucked 0x20 from, my other test run of code used 0x80 so I'm confused how I made that mistake. And yes 32 bit, trying to keep it compatible with my other system.

For the record this is what I'm using to assemble/compile

Code:
nasm -f elf str.asm
gcc -o output str.o main.c

and main.c

Code:
int main(int argc, char **argv)
{
	int ret_status;
	ret_status = str_asm();
	return ret_status;
}

even using

Code:
segment .text

	[GLOBAL str_asm]
	
	str_asm:
		
;		STRING "Hello World"
		
		mov	ebx,0					; exit number
		mov	eax,1					; sys_exit
		int	0x20					; call kernel

segfaults, and I have no clue why.
 
Last edited:
Back
Top Bottom