assembly hello worlds [intel32]

There are numerous ways to write a hello world program in every programming language. Here are 6 written in 32b assembly using nasm

The top 2 are the std ways of writing this, all the rest are for schittz&gigglz

1-std
1.5-std (NASM style)
2-std using functions
3-writing 1 byte at a time with a forloop
4-pushing 1B, padded so it’s 4B aligned, onto stack, and then writing 1B at a time
5-null-terminated: write until 0x0 encountered
6-pushing string onto stack, and then writing from it

;Guandi97
;std method, the "correct" way to do this

section .text
global _main
_main:
	push 	len
	push	sdr
	push	0x1
	sub	esp,		0x4
	mov	eax,		0x4
	int	0x80
	add	esp,		0x10

	push	0x1
	sub	esp,		0x4		;always 8-byte aligned on intel32
	mov	eax,		0x1
	int	0x80
section .data
sdr	db	"noobie plz",0xa		;consecutive byte sequence, or char array
len	equ	$-sdr				;this is the same as the sizeof() in C

This is an easier way to output a string to the console.

 

;Guandi97
;function call method, the other "correct" way to do this

section .text
global _main
_main:
	push	dword 	sdr
	push 	dword 	len
	call	_rite
	add	esp,		0x8

	push	0x1
	sub	esp,		0x4		;always 8-byte aligned on intel32
	mov	eax,		0x1
	int	0x80
_rite:	push	ebp
	mov	ebp,	esp
	push	dword 	[ebp+0x8]
	push	dword 	[ebp+0xc]
	push	dword	0x1
	sub	esp,		0x4
	mov	eax,		0x4
	int	0x80
	add	esp,		0x10
	pop	ebp
	ret
section .data
sdr	db	"Lunar Republic",0xa
len	equ	$-sdr
;Guandi97
;using a forloop 

;calculate esi,edi
;push args onto stack
;write 1B at a time, and update pointer

section .text
global _main
_main:
	push	ebp
	mov	ebp,		esp	;00
	mov	esi,		sdr
	mov	edi,		esi
	add	edi,		len

	push	dword	0x1		;04
	push	dword 	0x0		;08
	push	dword	0x1		;12
	sub	esp,		0x4	;16

wloops	cmp	esi,		edi
	jge	wloope
	mov	[ebp-0x8],	esi
	mov	eax,		0x4
	int 	0x80
	add	esi,		0x1
	jmp	wloops

wloope	add	esp,		0x10
	push	dword	0x1
	sub	esp,		0x4
	mov	eax,		0x1
	int	0x80
section .data
sdr	db	'twilicorn OP',0xa
len	equ	$-sdr
;Guandi97
;push method

;calculate len 
;push all onto stack
;write 1B at a time

section .text
global _main
_main:
	mov	esi,		sdr
	mov	edi,		esi
	add	esi,		len
ploops	cmp	esi,		edi		;push onto stack
	jl	ploope
	mov	edx,		0x0
	mov	dl,		byte [esi]
	push	edx
	sub	esi,		0x1
	jmp	ploops
ploope	mov	ecx,		0x0
wloops	cmp	ecx,		len		
	jge	wloope
	push	dword 	0x1			;-04
	mov	esi,	esp
	add	esi,	0x4
	push	dword 	esi			;-08			
	push	dword 	0x1			;-12
	sub	esp,		0x4		;-16
	mov	eax,		0x4
	int	0x80
	add	esp,		0x14
	add	ecx,		0x1
	jmp	wloops
wloope	push	dword 	0x1
	sub	esp,		0x4
	mov	eax,		0x1
	int	0x80
section .data
sdr	db	'engieUNion!',0xa
len	equ	$-sdr
;Guandi97
;null terminated while loop

;set edi,esi
;check against 0x0
;write 1B at a time

section .text
global _main
_main:
	push	ebp
	mov	ebp,		esp
	mov	esi,		sdr
	push	dword 	0x1			;04
	push	dword	0x0
	push	dword	0x1
	sub	esp,		0x4
	mov	edi,		ebp
	sub	edi,		0x8
cloops	cmp	byte [esi],	0x0
	je	cloope
	mov	[edi],		esi
	mov	eax,		0x4
	int	0x80
	add	esi,		0x1
	jmp	cloops
cloope	mov	[esp],		dword 0x1
	sub	esp,		0x4	
	mov	eax,		0x1
	int	0x80
section .data
sdr	db	'PootisSpencer here!',0xa,0x0
;Guandi97
;stack byte array method: this method has almost no application irl

;determine esi,edi
;loop esi
;call _byteSStack
;"flush" ebx to stack
;set args, call write

;_byteSStack (8 bit)
	;store in ebx(buffer), accum in eax,
	;shift and add to bl
;_Bflush
	;determin the number of shifts needed to align the fist byte in with the most significant bit in ebx
	;loop shifts

section .text
global _main
_main:
	push	ebp
	mov	ebp,		esp

	mov	edi,		sdr
	mov	esi,		edi
	add	esi,		len
	mov	ebx,		0x0
aloop1s	cmp	esi,		edi
	jle	aloop1e
	call	_byteSStack
	cmp	eax,		0x4		;if eax is full, flush immediately
	jge	bsPush
	jmp	aloop1s
bsPush	push	dword	ebx
	mov	ebx,		0x0
	mov	eax,		0x0
	jmp	aloop1s

aloop1e	cmp	eax,		0x0
	jne	flush
	jmp	end
flush	call	_Bflush				;need to flush manually
	push	dword	ebx
end	push	len		
	mov	edx,		esp
	add	edx,		0x4
	add	edx,		eax
	push	dword 	edx
	push	0x1
	sub	esp,		0x4
	mov	eax,		0x4		;since last 2 args on stack are the same needed for sys_exit(1), interrupt
	int	0x80				;demonstration only, this is bad practice,
	mov	eax,		0x1		;it will confuse both future you, and everybody else who views your code
	int	0x80

_byteSStack:
	sub	esi,		0x1
	shl	ebx,		8		;shift 8b to left
	mov	bl,		byte [esi]
	add	eax,		0x1
	ret
_Bflush:
	mov	edx,		0x4
	sub	edx,		eax
	mov	eax,		edx
	mov	ecx,		0x0
floops	cmp	ecx,		eax		;shift until aligned
	jge	floope
	shl	ebx,		0x8		;shift 8b to left
	add	ecx,		0x1
	jmp	floops
floope	ret
section .data
sdr	db	"AnonyMOOOOOOOOOOooooooooooose",0xa,
len	equ	$-sdr

Leave a Reply

Your email address will not be published. Required fields are marked *