weblog d’un abbe


Back in .S world

Filed under: Hacking — abbe @ 1743

After staying away from assembly language for one year, I’m back :). And my first piece of code is a partial touch for Linux AMD64 architecture to debug 2 bugs (249878, 250066), I recently filed in Fedora 7 Bugzilla. Anyways, following is the gas code, I hacked:

/* touchs.S */
.globl _start

.section .text

#define SCR    %rax
#define RETVAL %rax
#define ARG0   %rdi
#define ARG1   %rsi
#define ARG2   %rdx
#define ARG3   %r10
#define ARG4   %r8
#define ARG5   %r9

.p2align 4
	mov $1, SCR

.p2align 4
	mov $60, SCR

.p2align 4
	mov $2, SCR

.p2align 4
	mov $3, SCR

.p2align 4
	xorb %al, %al
	xorq %rcx, %rcx
	decq %rcx
	repne scasb
	notq %rcx
	decq %rcx

.p2align 4
	leaq 8(%rsp), %rbp

	mov  (%rsp), %r12           /* load argc into r12 */
	cmp  $2, %r12
	jne  2f                     /* if(argc != 2) goto 3 */

	mov  8(%rbp), ARG0          /* filename */
	mov  $(01|0100|01000), ARG1 /* O_WRONLY | O_CREAT | O_TRUNC */
	mov  $(0600), ARG2          /* File mode */
	call _open                  /* fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600) */
	cmpq $(~1), RETVAL           
	jle  1f                     /* if (fd <= -1) goto 1 */

	mov  RETVAL, ARG0           /* fd */
	call _close                 /* close(fd) */

	xorq ARG0, ARG0             /* 0 */
	call _exit                  /* exit(0) */

1:	notq RETVAL                 /* undo */
	incq RETVAL                 /* 2's complement */
	mov  RETVAL, ARG0           /* -fd */
	call _exit                  /* exit(-fd) */

2:	leaq usage, %rdi
	call strlen
	mov  %rcx, ARG2
	mov  $1, ARG0
	leaq usage, ARG1
	call _write
	xorq ARG0,ARG0
	call _exit

.p2align 4
      .ascii "Usage: touchs [file-name-to-be-touched]"
      .byte  10
      .ascii "Touches the file in sameway as sudo. "
      .ascii "Error if any, will be reported in exit code."
      .word  10

Following is the command line to compile the code:

[wahjava@chatteau ~]$ gcc -g -o touchs -nostdinc -nostdlib touchs.S

And to run it:

[wahjava@chatteau ~]$ ./touchs "/tmp/_dc7u4C" && ls -l "/tmp/_dc7u4C"

So, happy hacking in .S world :-D



  1. Really glad to see this! Reminds me of the MS-DOS days.. TASM, Interrupts, Yashwant Kanetkar etc. Feeling nostalgic already.. HEHE!

    Comment by Edwin Jose Palathinkal — 20070815 @ 1354

  2. Yashwant Kanetkar is a crap, I never bought any of his book, and always kept myself away from his influence.

    Comment by आशीष शुक्ल — 20070815 @ 1430

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: