//----------------------------------------------------------------- // upperrax.s // // This program lets us see the "side-effect" upon the upper // half of register RAX of an operation upon the lower half. // // to assemble: $ as upperrax.s -o upperrax.o // and to link: $ ld upperrax.o -o upperrax // and execute: $ ./upperrax // // programmer: ALLAN CRUSE // written on: 11 APR 2009 //----------------------------------------------------------------- .equ STDOUT_ID, 1 .equ sys_WRITE, 1 .equ sys_EXIT, 60 .section .data val1: .quad 0x0123456789ABCDEF val2: .quad 0x0000000000000000 msg1: .ascii "\n RAX=" buf1: .ascii "xxxxxxxxxxxxxxxx (Before) \n" len1: .quad . - msg1 msg2: .ascii "\n RAX=" buf2: .ascii "xxxxxxxxxxxxxxxx (After) \n\n" len2: .quad . - msg2 hex: .ascii "0123456789ABCDEF" .section .text _start: # operation on lower-half of RAX mov val1, %rax mov %eax, %eax # <-- does this affect upper bits? mov %rax, val2 # format value #1 mov val1, %rax lea buf1, %rdi call rax2hex # display value #1 mov $sys_WRITE, %rax mov $STDOUT_ID, %rdi lea msg1, %rsi mov len1, %rdx syscall # format value #2 mov val2, %rax lea buf2, %rdi call rax2hex # display value #2 mov $sys_WRITE, %rax mov $STDOUT_ID, %rdi lea msg2, %rsi mov len2, %rdx syscall # program termination mov $sys_EXIT, %rax xor %rdi, %rdi syscall .global _start # The following procedure converts the quadword found in register RAX # to its representation as a string of hexadecimal numerals at (RDI). rax2hex: push %rax push %rbx push %rcx push %rdx push %rdi mov $16, %rcx nxnyb: rol $4, %rax mov %al, %bl and $0xF, %rbx mov hex(%rbx), %dl mov %dl, (%rdi) inc %rdi loop nxnyb pop %rdi pop %rdx pop %rcx pop %rbx pop %rax ret .end