• Get the Middle Character


    You are going to be given a word. Your job is to return the middle character of the word. If the word's length is odd, return the middle character. If the word's length is even, return the middle 2 characters.

    #Examples:

    Kata.getMiddle("test") should return "es"
    
    Kata.getMiddle("testing") should return "t"
    
    Kata.getMiddle("middle") should return "dd"
    
    Kata.getMiddle("A") should return "A"
    

    #Input

    A word (string) of length 0 < str < 1000 (In javascript you may get slightly more than 1000 in some test cases due to an error in the test cases). You do not need to test for this. This is only here to tell you that you do not need to worry about your solution timing out.

    #Output

    The middle character(s) of the word represented as a string.

    自己的编译都无法通过的垃圾代码

    section .text
    global get_middle
    extern strlen
    extern malloc
    ; void get_middle(const char *s, char *buf)
    ; Write the result to `buf`. Don't forget to add a null byte.
    get_middle:
      call strlen
      mov rcx,rax
      
      cmp rcx,0
      je .st2
      
      mov rsi,rdi
      and rcx,1
      
      cmp rcx,1
      je .st1
      
      
      mov rcx,rax
      shr rcx,2
      mov rdi,3
      call malloc
      mov byte [rdi],byte [rsi+rcx-1]
      mov byte [rdi+1],byte [rsi+rcx]
      mov byte[rdi+2],0
      mov buf,rdi
      jmp .st2
      .st1:
      mov rcx,rax
      shr rcx,2
      mov rdi,2
      call malloc
      mov byte[rdi],byte[rsi+rcx]
      mov byte[rdi+1],0
      mov buf,rdi
      
      .st2:
      ret

    大神的代码

    section .text
    global get_middle
    ; void get_middle(const char *s, char *buf)
    ; Write the result to `buf`. Don't forget to add a null byte.
    get_middle:
      xor rcx, rcx
      dec rcx ; rcx = 0xFFFFFFFFFFFFFFFF
      xor rax, rax ; rax = 
      mov [rsi], rax ; buf = {0, 0, 0,}
      push rdi
      cld
      repne scasb
      pop rdi ; rdi = s
      neg rcx
      sub rcx, 2 ; rcx = strlen(s)
      mov rax, rcx
      xor rdx, rdx ; rdx:rax = strlen(s)
      mov rbx, 2
      div rbx ; rax = strlen(s) / 2
      mov bl, [rdi + rax] ; rbx = center or right symbol
      test rcx, 1
      jnz return ; if (rcx & 1 == 0) even
      mov [rsi + 1], bl
      mov bl, [rdi + rax - 1] ; rbx = left symbol
    return:
      mov [rsi], bl
      ret

    第二个

    extern strlen
    
    section .text
    global get_middle
    ; void get_middle(const char *s, char *buf)
    get_middle:
                    push    rdi
                    push    rsi
                    push    rbp
                    call    strlen
                    pop     rbp
                    pop     rsi
                    pop     rdi
                    dec     rax
                    shr     rax, 1
                    sbb     ecx, ecx
                    mov     dl, [rdi + rax]
                    mov     [rsi], dl
                    and     cl, [rdi + rax + 1]
                    mov     [rsi + 1], cl
                    mov     [rsi + 2], byte 0                
                    ret

    第三个

    section .text
    global get_middle
    ; void get_middle(const char *s, char *buf)
    ; Write the result to `buf`. Don't forget to add a null byte.
    get_middle:
      call get_length
      mov rcx, 2
      cqo ;zero rdx for division instruction
      idiv rcx
      add rdi, rax;move to the halfway point
      add rdi, rdx;if it's odd, moves to the ACTUAL halfway point
      dec rdi; 0 index
      mov al, BYTE [rdi]
      mov BYTE [rsi], al;get our first character
      test rdx, rdx
      jnz finish
      inc rsi
      mov al, BYTE [rdi+1]
      mov BYTE [rsi], al;get the second character
      finish:
        mov BYTE [rsi+1], 0
      ret
    get_length:
      push rdi
      xor rax, rax
      bg_loop:
        xor rbx, rbx
        cmp BYTE [rdi], 0
        setne bl
        movzx rbx, bl
        add rax, rbx
        inc rdi
        test rbx, rbx
        jnz bg_loop
      pop rdi
      ret

     第四个

    section .text
    global get_middle
    get_middle:
      xor   rax,rax
      mov   rcx,-1
      repne scasb
      dec   rcx
      sar   rcx,1
      mov   ax,[rdi+rcx]
      mov   [rsi],ax
      adc   rsi,1
      mov   byte[rsi],0
      ret

    周日下午全部都看一遍  在重写一遍

  • 相关阅读:
    base64加密后字符串长度
    Mysql如何查字段的长度,Mysql中length()、char_length()的区别
    Java 逆变与协变的名词说明
    Java 泛型在实际开发中的应用
    MySQL自增主键排序问题
    【python基础】怎么实现装饰器?
    【python基础】列表推导式
    【python】爬虫实践项目(一):39问医生
    关于DRF序列化器模型字段参数设置报错AssertionError: May not set both `read_only` and `write_only`
    DRF学习笔记
  • 原文地址:https://www.cnblogs.com/pppyyyzzz/p/13652318.html
Copyright © 2020-2023  润新知