assembly - 无法弄清楚程序集 x86 中的 printf 函数以及使用操作数

标签 assembly x86 printf nasm exponentiation

我已经对发布这个问题感到紧张了,但事情就这样了。我试图设计一个汇编程序来接受两个整数,然后接受一个操作数(*、+ 或 ^)。然后,根据操作数,程序将根据所选操作数执行算术序列。输出必须看起来完全像这样,假设用户输入 5,然后用户输入 6,最后用户输入 *,程序必须打印出来

OP 5 * 6 = 30

现在,由于某种原因我无法理解 printf 的概念,所以我底部的 printf 函数是我试图打印出来的荒谬方式。如果有人可以向我解释 printf 是如何工作的,我很乐意听到!我对堆栈有非常基本的了解。另外,我没有指数函数,因为我不知道如何用汇编语言做到这一点。最后,我有一个与程序无关的问题要问真正的计算机之神......我绝对讨厌汇编语言,我在用这种语言编写基本操作时遇到困难。我喜欢用 C、CPP 和 Java 工作……这种语言对于程序员的日常生存来说是绝对必要的吗?还是这是一堂关于人们在拥有打火机之前如何生火的历史课?无论如何,欢迎回答任何问题,提前谢谢您。

;program to add, multiply, or give the power of a number

%include "asm_io.inc"


segment .data 


prompt1 db    "Enter a number: ", 0        ;prompts
prompt2 db    "Enter another number: ", 0
prompt3 db    "Enter an operand", 0
prompt4 db    "OP ", 0    ;idiotic prompts for printing
prompt5 db    " = ", 0    ;another printing prompt

segment .bss

num1  resd 1    ;variable for 1st number
num2  resd 1    ;variable for 2nd number
op    resd 1    ;variable for operand


segment .text

    global  asm_main
asm_main:
    enter   0,0               ; setup routine
    pusha

    restart:
        mov     eax, prompt1      ; print out prompt
        call    print_string
        call    print_nl

        call    read_int          ; read integer
        mov     [num1], eax       ; store into num1
        call    print_nl

        mov     eax, prompt2      ; print out prompt
        call    print_string
        call    print_nl

        call    read_int          ; read integer
        mov     [num2], eax     ; store into num2
        call    print_nl

        mov     eax, promtp3    ;print out prompt
        call    print_string
        call    print_nl

        call    read_char       ;read operand and dispose of null
        call    read_char
        mov     [op], eax       ;store operand in [op]
        call    print_nl

        cmp     [op], '*'       ;operand comparison
        jne     jmp1
        call    mulFun
        jmp     restart

jmp1:   cmp     [op], '+'       ;operand comparison
        jne     jmp2
        call    sumFun
        jmp     restart

jmp2:   cmp     [op], '^'       ;operand comparison
        jne     jmp3
        call    expFun
        jmp     restart

jmp3:   cmp     [op], 'e'       ;exit function
        je      end1

end1:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    call    print_nl          ; pr
    popa
    mov     eax, 0            ; return back to C
    leave
    ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
mulfun:                     ;multiplication function
    mov     ebx, [num1]
    mov     ecx, [num2]
    imul    ebx, ecx
    jmp     printFun

sumFun:                     ;sum function
    mov     ebx, [num1]
    mov     ecx, [num2]
    add     ebx, ecx
    call    printFun


printFun:                   ;printing function
    mov     eax, [prompt4]
    call    print_string
    mov     eax, [num1]
    call    print_string
    mov     eax, [op]
    call    print_string
    mov     eax, [num2]
    call    print_string
    mov     eax, [prompt5]
    call    print_string
    mov     eax, ebx
    call    print_string
    call    print_nl
    ret

最佳答案

凡是我发现问题的地方,我都会在评论中添加***。有几点需要注意:

  • 您的某些标签中有拼写错误
  • NASM 区分大小写,因此 mulfunmulFun 不同。
  • 如果您的指令采用一个或多个操作数,不涉及寄存器(用于源或目标)但引用内存 - 您需要指定内存操作数的大小。例如,cmp [op], '+' 必须指定 [op] 引用的数据大小,以便在其前面加上大小前缀。大小可以是字节双字。您正在比较单个字符,因此大小将为 byte 。代码应该如下所示 cmp byte [op], '+'
  • 如果您有一个变量,并且执行mov eax, varname,这会将varname的地址移动到EAX。如果要移动 varname 的内容,则必须用方括号将其括起来,例如 mov eax, [varname] 。当处理字符串地址和 print_string 例程时,您需要在 EAX 中传递字符串的地址,而不是内容。所以省略括号。
  • 如果您使用 call 调用函数,您的函数应以 ret 结尾。
  • 不要jmp到函数,使用call
  • 如果要打印字符,请不要使用print_string,而是使用print_char。将字符放入EAX
  • 如果要打印整数,请不要使用 print_string,而使用 print_int。将整数放入EAX

修改后的代码如下:

;program to add, multiply, or give the power of a number

%include "asm_io.inc"

segment .data

prompt1 db    "Enter a number: ", 0        ;prompts
prompt2 db    "Enter another number: ", 0
prompt3 db    "Enter an operand", 0
prompt4 db    "OP ", 0    ;idiotic prompts for printing
prompt5 db    " = ", 0    ;another printing prompt

segment .bss

num1  resd 1    ;variable for 1st number
num2  resd 1    ;variable for 2nd number
op    resd 1    ;variable for operand


segment .text

    global  asm_main
asm_main:
    enter   0,0                 ; setup routine
    pusha

    restart:
        mov     eax, prompt1    ; print out prompt
        call    print_string
        call    print_nl

        call    read_int        ; read integer
        mov     [num1], eax     ; store into num1
        call    print_nl

        mov     eax, prompt2    ; print out prompt
        call    print_string
        call    print_nl

        call    read_int        ; read integer
        mov     [num2], eax     ; store into num2
        call    print_nl

        mov     eax, prompt3    ;print out prompt
                                ;*** Typo - promtp3 changed to prompt3
        call    print_string
        call    print_nl

        call    read_char       ;read operand and dispose of null
        call    read_char
        mov     [op], eax       ;store operand in [op]
        call    print_nl

        cmp     byte [op], '*'  ;operand comparison
                                ;*** We must specify the size of data we are
                                ;*** comparing, so we tell the assembler that
                                ;*** with 'byte' in front of the variable
        jne     jmp1
        call    mulFun
        jmp     restart

jmp1:   cmp     byte [op], '+'  ;operand comparison
                                ;*** We must specify the size of data we are
                                ;*** comparing, so we tell the assembler that
                                ;*** with 'byte' in front of the variable
        jne     jmp2
        call    sumFun
        jmp     restart

jmp2:   cmp     byte [op], '^'  ;operand comparison
                                ;*** We must specify the size of data we are
                                ;*** comparing, so we tell the assembler that
                                ;*** with 'byte' in front of the variable
        jne     jmp3
;        call    expFun         ;*** This expFun function doesn't exist so
                                ;*** don't call it so we can compile and link
        jmp     restart

jmp3:   cmp     byte [op], 'e'  ;exit function
                                ;*** We must specify the size of data we are
                                ;*** comparing, so we tell the assembler that
                                ;*** with 'byte' in front of the variable
        je      end1

end1:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    call    print_nl          ; pr
    popa
    mov     eax, 0            ; return back to C
    leave
    ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
mulFun:                     ;multiplication function
                            ;*** NASM is case sensitive - changed mulfun to mulFun
    mov     ebx, [num1]
    mov     ecx, [num2]
    imul    ebx, ecx
    call    printFun        ;*** Call printFun, don't 'jmp' to it
    ret                     ;*** Since mulfun was called, we use 'ret' to return

sumFun:                     ;sum function
    mov     ebx, [num1]
    mov     ecx, [num2]
    add     ebx, ecx
    call    printFun
    ret

printFun:                   ;printing function
    mov     eax, prompt4    ;*** We want the address of prompt4, not what is at prompt4
                            ;*** Remove the brackets from [prompt4]
    call    print_string
    mov     eax, [num1]
    call    print_int       ;*** Use print_int to print an integer, not print_string
    mov     eax, [op]
    call    print_char      ;*** Use print_char to print a char, not print_string
    mov     eax, [num2]
    call    print_int       ;*** Use print_int to print an integer, not print_string
    mov     eax, prompt5    ;*** We want the address of prompt5, not what is at prompt5
                            ;*** Remove the brackets from [prompt5]
    call    print_string
    mov     eax, ebx
    call    print_int       ;*** Use print_int to print an integer, not print_string;
    call    print_nl
    ret

我将把expFun函数的开发留给原发者。目前我已将其注释掉,以便可以组装、链接和运行现有代码。

您可以使用printf,但我认为漫长的道路也可以教会一些东西,并且我从我的答案中排除了printf的讨论。您的讲师、类(class) Material 、助教将是您提出其他问题的好地方。

提供此答案是为了让您的程序进入可用状态,以便您可以继续作业。在我看来,原来的问题太宽泛了。如果需要进一步的帮助,最好针对特定问题提出新问题。

关于assembly - 无法弄清楚程序集 x86 中的 printf 函数以及使用操作数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33510285/

相关文章:

c - 为什么在引用 int 变量的大小时必须使用 %ld?

c++ - C++会先转换成汇编吗

c - 从汇编逆向工程 C 源代码

c++ - 到达for循环时出现奇怪的段错误

windows - 汇编:如何更改此代码以请求用户输入

c - snprintf 困惑

assembly - or32-uclinux-gcc 给出汇编消息 : no such instruction

c# - x64 和 x86 之间字节数组访问的巨大性能差异

gcc - 声明“extern struct cpu * cpu asm (“%gs:0”)”是什么?意思是?

c - 此输出背后的原因