Logo  

CS456 - Systems Programming

Displaying exercises/e4/files/argv.s

%include "lib.h"

extern putc, puts, exit

; To compile: nasm -felf64 argv.s
; To link   : ld -o argv argv.o lib.o

SECTION .text

global _start
_start:
	mov	r14, 0		; r14 = 0
	mov	r15, [rsp]	; r15 = [argc];

.loop:				; do {
	inc	r14
	mov	rsi, [rsp+r14*8]  ; rsi = argv[r14-1]
	
	call	puts		;   puts(rsi)
	mov	al, `\n`
	call	putc		;   putc('\n');
	dec	r15
	jnz	.loop		; } while (r15 > 0);

	inc	r14		; skips past the last argv (that points to NULL)

	; put a jmp .done here if you do not wish to print the environment.

; Print the environment (envp):
.loop2:				; while (rsi != NULL) {
	inc	r14
	mov	rsi, [rsp+r14*8]  ; rsi = envp[r14-argc]
	cmp	rsi, 0
	je	.done
	call	puts
	mov	al, `\n`
	call	putc		;    putc('\n');
	jmp	.loop2		; }

.done:
	mov	rdi, 0
	call	exit