如何在x86汇编语言中编写自己的atoi函数

时间:2019-04-08 02:19:59

标签: linux assembly x86-64 att atoi

我在汇编中编写自己的atoi函数时遇到了麻烦。说明是

“更改函数,使其返回传递给函数的C字符串(指针)的整数等效值。您可以假定第一个字符在'0'和'9'之间(包括0和9)。从第一个字符到第一个字符(不是十进制数字)的所有字符。如您所见,mains使用atoias返回的值作为退出代码(这是访问atoi输出的廉价方法,而无需编写itoafunction ) 如给您的一样,atoireturns为1234。返回值与0xFF进行ANDed运算,以将其减小为一个字节。因此1234&255变为210。”

    # Useful constants 
    .equ    STDIN,0 
    .equ    STDOUT,1 
    .equ    READ,0 
    .equ    WRITE,1 
    .equ    EXIT,60 
# Stack frame 
    .equ    bufferSize, 32
    .equ    buffer,-bufferSize
    .equ    localSize,16 
    .equ    frameSize, bufferSize + localSize
# Read only data 
    .section    .rodata # the read-only data section 
prompt: 
    .string     "Enter an integer: " 
    .equ    promptSz,.-prompt-1 
msg: 
    .string     "You entered: " 
    .equ    msgSz,.-msg-1 

代码

    .text   # switch to text section 


    .globl  __start 
 __start: 
    pushq   %rbp    # save caller’s frame pointer 
    movq    %rsp, %rbp  # establish our frame pointer 
    subq    $frameSize, %rsp    # for local variables 

    movl    $promptSz, %edx # prompt size 
    movl    $prompt, %esi   # address of prompt text string 
    movl    $STDOUT, %edi   # standard out 
    movl    $WRITE, %eax 
    syscall     # request kernel service 

    movl    $bufferSize,%edx
    leaq    buffer(%rbp), %rsi  # load buffer address
    movl    $STDIN, %edi    # standard in 
    movl    $READ, %eax 
    syscall     # request kernel service 
    movl    %eax, (%rsp)    # store num chars read

    leaq    buffer(%rbp), %rsi  # load buffer address
    call    atoi    # our exit code will be the return from atoi

    movq    %rbp, %rsp  # delete local variables 
    popq    %rbp    # restore caller’s frame pointer 
    movl    %eax, %edi  # put exit status in %edi (will be ANDed with FF)
    movl    $EXIT, %eax # exit from this process 

    syscall

基本代码如下所示,我只需要实现自己的atoi。到目前为止,我对atoi函数的功能是

atoi:
    pushq   %rbp    # save caller’s frame pointer 
    movq    %rsp, %rbp  # establish our frame pointer 
    subq    $16, %rsp   # for local variables

    movq    %rdi, -16(%rbp) #moving first argument to local variable
    movl    $0, -4(%rbp) #moving 0 to local variable
    movl    $10, -12(%rbp) #moving 10 to local variable

    movl    -16(%rbp), %rax
    movzbl  (%rax), %eax #getting value of rax
    movl    -4(%rbp), %eax

    imull   -12(%rbp), %eax
    movl    %eax,   -4(%rbp)

    movq    %rbp, %rsp  # delete local variables 
    popq    %rbp    # restore caller’s frame pointer 
    ret
我不知下一步该去哪里。看来我所做的一切只会给我带来细分错误

1 个答案:

答案 0 :(得分:0)

您正在过度使用局部变量(而未充分使用寄存器);当发现无效字符时,将需要一个停止的循环;并且可能使用了错误的调用约定(系统调用看起来像Linux,这意味着System V AMD64 ABI,这意味着参数是通过寄存器而不是堆栈传递的。)

请注意,这完全可以不需要任何局部变量。例如(NASM语法,因为我不做AT&T,未经测试):

;Convert string to integer
;
;Input
; rdi = first parameter (address of string)
;
;Output
; rax = result

atoi:
    xor rax,rax               ;rax = 0 (this will become the returned result)
.nextChar:
    movzx rcx,byte [rdi]      ;rcx = next character
    sub rcx,'0'               ;rcx = value of next digit
    jb .done                  ;Invalid character (too low to be a decimal digit)
    cmp rcx,9                 ;Was it too high to be a decimal digit?
    ja .done                  ; yes, invalid

    lea rax,[rax*4+rax]       ;rax = result*5
    lea rax,[rax*2+rcx]       ;rax = result*5*2 + digit = result*10 + digit
    inc rdi                   ;rdi = address of next character
    jmp .nextChar
.done:
     ret

注意:此代码不适用于负值(例如,以'-'开头的字符串),并且在结果溢出时不会返回错误条件。结果也将是64位(而int可能是32位)。通常,这是“转换为unsigned long long”(错误处理就像atoi()一样糟糕。)