linux - itoa 汇编实现,div 操作导致segfault?

标签 linux assembly nasm x86-64

<分区>

所以我正在尝试实现 itoa,它将 int 转换为字符串。 到目前为止,如果我不在 .loop 部分中循环并坚持使用小数字,则该实现是有效的。一旦它循环,我的程序就会出现段错误。

代码如下:

section .data
    buffer  times 11 db 0

section .text
    global  ft_itoa
    extern  ft_strrevd
    extern  malloc

ft_itoa:
    mov     rcx, 1                      ;initialize our counter at 1 for the terminating null byte
    mov     rax, rdi                    ;move number in RAX for DIV instruction
    push    rbx                         ;save RBX
    mov     bl, 10

.check_negative:
    and     edi, 0xf0000000
    mov     rdi, buffer
    jz      .loop                       ;number is positive, proceed to main loop
    not     rax                         ;else
    inc     rax                         ;compute absolute value with binary complement
    mov     r9, 1                       ;set neg flag

.loop:
    cmp     rax, 0
    jz      .check_neg_flag
    div     bl
    add     ah, 48                      ;convert int to char
    mov     byte[rdi + rcx - 1], ah     ;copy char in buffer
    sub     ah, 48
    inc     rcx
    jmp     .loop                       ;commenting this line prevents crash

.check_neg_flag:
    cmp     r9, 1
    jne     .dup
    mov     byte[rdi + rcx - 1], '-'
    inc     rcx

.dup:
    mov     byte[rdi + rcx - 1], 0
    call    ft_strrevd                  ;copy buffer string in memory and return pointer

.end:
    pop     rbx                         ;restore RBX
    ret

这很可能是由 div 引起的,但我无法理解它的工作原理。 如果有人能指出我的解决方案,我们将不胜感激。

最佳答案

所以为了解决这个问题,我必须使用 div ebx 而不是 div bl,并且在每个 div 之前使用 xor edx、edx。

这是一个工作版本:

section .data
    buffer  times 11 db 0

    nega db "neg",0
    posi db "pos",0

section .text
    global  ft_itoa
    extern  ft_strdup
    extern  malloc

ft_itoa:
    xor     rcx, rcx                    ;initialize counter
    xor     r9, r9                      ;set neg flag to 0
    mov     eax, edi                    ;move number in RAX for DIV instruction
    push    rbx                         ;save RBX
    mov     ebx, 10

.check_negative:
    and     edi, 0x80000000
    mov     rdi, buffer
    jz      .divide                     ;number is positive, proceed to main loop
    not     eax                         ;else
    inc     eax                         ;compute absolute value with binary complement
    inc     r9                          ;set neg flag

.divide:
    xor     edx, edx
    div     ebx
    add     edx, 48                     ;convert int to char
    push    rdx
    inc     rcx
    cmp     eax, 0
    jnz     .divide

.check_neg_flag:
    cmp     r9, 1
    jne     .buff_string
    mov     byte[rdi], '-'

.buff_string:
    pop     rdx
    mov     byte[rdi + r9], dl
    dec     rcx
    inc     r9
    cmp     rcx, 0
    jnz     .buff_string

.dup:
    mov     byte[rdi + r9], 0
    call    ft_strdup                   ;copy buffer string in memory and return pointer
    pop     rbx                         ;restore RBX
    ret

关于linux - itoa 汇编实现,div 操作导致segfault?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54380755/

相关文章:

linux - 如何将 `config.sh` 中的变量包含到 `a.sh` 和 `b.sh` 中?

linux - numa_police_内存

debugging - 用于 32 位 x86 汇编的 DOS 调试程序

c++ - 如何通过取消引用内存中的另一个地址来取消引用内存中的特定地址?

linux - 在 Linux 下执行平面二进制文件

c - linux中汇编和C的混合编程

python - Heroku:将文件放入 bin 文件夹

c++ - Linux 上的 PThreads 和多核 CPU

linux - shell中的cmd1 & cmd2 和 cmd1 && cmd2 有什么区别

c - 传递参数 C -> NASM -> C