;\ 注释后的指令不执行?NASM 评论中的反斜杠有什么特别之处? [英] Instructions after a ;\ comment don't execute? What's special about backslash in comments in NASM?

查看:59
本文介绍了;\ 注释后的指令不执行?NASM 评论中的反斜杠有什么特别之处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 ASM 中编写了一个 ASCII 到十进制转换器,经过编辑,它没有添加到 rax,这导致了无限循环.GDB 表示 rax 不接受任何数字,即使从内存和其他寄存器中移出也是如此.

I was programming an ASCII to decimal converter in ASM and after an edit, it just didn't add to rax, this caused an infinite loop. GDB says that rax wasn't accepting ANY numbers, even when moved from memory and other registers.

我试过用 mov 把东西移到 rax 中,我也试过 inc 操作.

I have tried moving things into rax with mov and I have also tried the inc operation.

这是 asciiToDec 函数,注释得很好,我将 ASCII 数字的位置,然后将它们的数量压入堆栈,然后调用此函数.

Here is the asciiToDec function, well commented, I push the location of the ASCII numbers then the amount of them onto the stack then call this function.

    asciiToDec:                     ; Converts a number in ASCII to decimal that the computer can understand
        pop rax                     ; Remove the ret pointer so that it isnt popped off later when the other values are popped
        pop qword[amount]           ; Remove the amount of characters from the stack
        pop qword[location]         ; Remove the location from the stack
        push rax                    ; Push the ret pointer back onto the stack
        mov rax,1                   ; Move 1 into rax since 0*x==0 so 1*x==10 where x=10
        mov rbx,10                  ; Move 10 into rbx
        mov rcx,[amount]            ; Move the amount of stuff into rcx
        sub rcx,1                   ; Subtract one since the one's digit needs to be multiplied by 1 not 10
;-----------------------------------;
        loop1:                      ; Stage 1: Counts the power of 10 that is needed for the first number, highest digit
            mul rbx                 ; Multiply rax by rbx, rax*10
            sub rcx,1               ; Subtract 1 from rcx
            cmp rcx,0               ; Test if rcx==0
            jne loop1               ; Repeat if rcx>0
            mov [power10],rax       ; Move the power of ten into the power10 variable
            xor rax,rax             ; Set rax to 0 via xoring the bits to 0
            mov rbx,[location]      ; Move the location of the ASCII into rbx
;-----------------------------------;
        loop2:                      ; Stage 2: Actually converts the ASCII to decimal by subtracting ASCII '0'
            xor rcx,rcx             ; Remove previous data
            mov cl,byte[rbx+rax]    ; Copy new data into the low byte of rcx
            cmp cl,10               ; Next 4 lines: test for newlines and carrige returns, shouldn't have any but just making sure
            je  loop2               ; /\
            cmp cl,13               ; /\
            je  loop2               ; /\
            add rax,1               ; INC rax so we have the next byte
            sub cl,'0'              ; Make it decimal and not ASCII
            cmp cl,9                ; Test if the value in cl is equal to 9
            jg  failFun             ; If greater than 9 then the ASCII value in this index was not 0-9
            push rax                ; Get the data in rax out of the way so we can do some calculations with rax
            mov al,cl               ; Move the byte in cl to al, 0-9
            mul qword[power10]      ; Multiply rax (al is the lowest 8 bits of rax) by the power of 10
            mov rcx,rax             ; Move the val in rax back to rcx for later use
            mov rax,[power10]       ; Move the power of 10 to rax
            push rbx                ; Get the data in rbx out of the way, like rax
            mov rbx,10              ; Move 10 into rbx
            div rbx                 ; Divide rax (power of ten) by rbx (10) to get the next lower digit's power
            pop rbx                 ; Get back the data that was in rbx
            mov [power10],rax       ; Move the power of 10 back into the `power10` variable from rax
            pop rax                 ; Get rax's data back
            add [total],rcx         ; Add rcx to the total
            cmp rax,[amount]        ; Compare rax to the amount of characters
            jne loop2               ; If we haven't gone through all the characters, loop again
        pop rax                     ; Get the ret pointer out of the stack so I can have it on the top later
        push qword[total]           ; Move the total into the stack
        push rax                    ; and push the ret pointer back on top
        jmp clean                   ; Jump to clean
;-----------------------------------;
        failFun:                    ; Stage 3: An option for the function to go through if stage 2 fails
            push qword fail
            push qword failLen
            call print
            pop rax
            push qword 0
            push rax
            jmp clean

我希望它将 dec 数压入堆栈以便稍后弹出,但即使在添加操作和 mov 之后,rax 也永远不会高于 0rax,[power10] 导致无限循环,因为 rax 永远不会达到 [amount]GDB 输出:

I expect it to push the dec number onto the stack for popping later but instead, rax is never above 0 even after the add operation and the mov rax,[power10] which results in an infinite loop because rax never reaches [amount] GDB output:

Starting program: /path/to/executable/a.out 
1234

Breakpoint 1, loop2 () at common.asm:114
114             xor rcx,rcx             ; Remove previous data
(gdb) info reg rax
rax            0x0  0
(gdb) nexti
115             mov cl,byte[rbx+rax]    ; Copy new data into the low byte of rcx
(gdb) 
116             cmp cl,10               ; Next 4 lines: test for newlines and carrige returns, shouldn't have any but just making sure
(gdb) 
120             add rax,1               ; INC rax so we have the next byte
(gdb) 
121             sub cl,'0'              ; Make it decimal and not ASCII
(gdb) info reg rax
rax            0x0  0

推荐答案

NASM 被您的 /\ 注释混淆并忽略这些行.删除这些注释.*实际上反斜杠是行的延续,因此 nasm 认为以下几行是您评论的其余部分 - Jester

NASM is confused by your /\ comments and ignores the lines.Remove those comments. *Actually backslash is line continuation so nasm thinks the following lines are the rest of your comment - Jester

使用 \ 删除注释可以解决问题,因为 NASM 认为下一行是注释.VIM NASM 语法高亮不遵循此规则.

Removing the comment with a \ fixes the issue since NASM thinks the next lines are comments. VIM NASM syntax highlighting does not follow this rule.

来自 NASM 手册,3.1 NASM 源代码行的布局:

From the NASM manual, 3.1 Layout of a NASM Source Line:

NASM 使用反斜杠 (\) 作为行继续符;如果一行以反斜杠结尾,下一行被认为是反斜杠结尾行的一部分.

NASM uses backslash (\) as the line continuation character; if a line ends with backslash, the next line is considered to be a part of the backslash-ended line.

这篇关于;\ 注释后的指令不执行?NASM 评论中的反斜杠有什么特别之处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆