Forums before death by AOL, social media and spammers... "We can't have nice things"
|    comp.lang.asm.x86    |    Ahh, the lost art of x86 assembly    |    4,675 messages    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
|    Message 3,151 of 4,675    |
|    aen@nospicedham.spamtrap.com to All    |
|    Palindromic number    |
|    06 Dec 17 11:24:17    |
   
   Hi!   
      
   Have I got this right from Agner Fog's Optimizing subroutines in   
   assembly language, pp 148-149? Especially do I assume the maximum   
   number correct?   
      
   .intel_syntax noprefix   
   # as -o posting.o posting.asm   
   # gcc -static -o posting posting.o   
    .data   
   format: .string "%llu\n"   
      
    .text   
    .globl main   
    .type main,@function   
   main: ; _start: nop   
    sub rsp,8   
      
   1: mov rdi,1234567890987654321   
    call ispal   
    jnz exit_ok   
      
    mov rdi,offset format   
    call printf # Reversed number in RSI.   
      
   exit_ok: xor rdi,rdi   
    mov rax,60   
    syscall   
    .size main,.-main   
      
    .data   
   # Optimizing subroutines in assembly, pp 148-149.   
   # r = w = 64, b = 3, max = 2^{r-b}-1   
   # max = 2,305,843,009,213,693,951   
    .balign 16   
   $r10: .quad 0x199999999999999a # Reciprocal for 10.   
      
    .text   
      
   # Calling sequence: call ispal   
   # Entry conditions: RDI = Integer to test.   
   # Exit conditions: Zero flag set if palindromic.   
      
    .balign 16   
    .type ispal,@function   
   ispal: mov rax,rdi   
    xor rsi,rsi   
   # Divide by 10.   
   0: push rax   
    mul qword ptr [$r10]   
    mov rax,rdx # RAX = quotient.   
    pop rdx   
    lea rcx,[rax+rax*4]   
    add rcx,rcx   
    sub rdx,rcx # RDX = remainder.   
   # Reverse.   
    lea rsi,[rsi+rsi*4]   
    lea rsi,[rdx+rsi*2]   
    test rax,rax   
    jnz 0b   
    cmp rdi,rsi   
    ret   
    .size ispal,.-ispal   
      
    .end   
   --   
   aen   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
(c) 1994, bbs@darkrealms.ca