• 《汇编语言 基于x86处理器》第八章高级过程部分的代码


    ▶ 输入 Count 个 32 位有符号整数,计算他们的和

    ● 代码,使用堆栈传参

     1 ;// Sum_main.asm,主过程
     2 INCLUDE Irvine32.inc
     3 
     4 EXTERN PromptForIntegers@0:PROC             ; 外部链接声明需要的函数
     5 EXTERN ArraySum@0:PROC, DisplaySum@0:PROC
     6 
     7 ArraySum            EQU ArraySum@0          ; 外部链接声明需要的变量
     8 PromptForIntegers   EQU PromptForIntegers@0
     9 DisplaySum          EQU DisplaySum@0
    10 
    11 Count = 3                                   ; 输入的变量数
    12 
    13 .data
    14 prompt1 BYTE  "Enter a signed 32-bit integer: ", 0
    15 prompt2 BYTE  "Sum: ", 0
    16 array   DWORD  Count DUP(?)
    17 sum     DWORD  ?
    18 
    19 .code
    20 main PROC
    21     call    Clrscr                          ; 清屏
    22 
    23 ; PromptForIntegers( addr prompt1, addr array, Count ),提示信息和输入整数
    24     push    Count
    25     push    OFFSET array
    26     push    OFFSET prompt1
    27     call    PromptForIntegers
    28 
    29 ; eax = ArraySum( addr array, Count ),数组求和
    30     push    Count
    31     push    OFFSET array
    32     call    ArraySum
    33     mov     sum,eax
    34 
    35 ; DisplaySum( addr prompt2, sum ),输出结果
    36     push    sum
    37     push    OFFSET prompt2
    38     call    DisplaySum
    39 
    40     call    Crlf
    41     call    WaitMsg
    42     exit
    43 main ENDP
    44 END main
     1 ;// _prompt.asm,提示信息和输入整数
     2 INCLUDE Irvine32.inc
     3 
     4 .code
     5 PromptForIntegers PROC
     6 arraySize   EQU [ebp+16]        ; 获取参数
     7 ptrArray    EQU [ebp+12]
     8 ptrPrompt   EQU [ebp+8]
     9     enter    0,0
    10     pushad
    11 
    12     mov     ecx, arraySize
    13     cmp     ecx, 0
    14     jle     L2
    15     mov     edx, ptrPrompt
    16     mov     esi, ptrArray
    17 
    18 L1:
    19     call    WriteString
    20     call    ReadInt
    21     call    Crlf
    22     mov     [esi], eax
    23     add     esi, 4
    24     loop    L1
    25 
    26 L2:
    27     popad
    28     leave
    29     ret     12                  ; 3 个参数
    30 PromptForIntegers ENDP
    31 END
     1 ;// _arrysum.asm,数组求和
     2 INCLUDE Irvine32.inc
     3 
     4 .code
     5 ArraySum PROC
     6 ptrArray    EQU [ebp+8]
     7 arraySize   EQU [ebp+12]
     8     enter   0, 0
     9     push    ecx
    10     push    esi
    11 
    12     mov     eax, 0           ; 使用循环对数组求和,放入 eax
    13     mov     esi, ptrArray
    14     mov     ecx, arraySize
    15     cmp     ecx, 0
    16     jle     L2
    17 
    18 L1:
    19     add     eax, [esi]
    20     add     esi, 4
    21     loop    L1
    22 
    23 L2:
    24     pop     esi
    25     pop     ecx
    26     leave
    27     ret     8
    28 ArraySum ENDP
    29 END
     1 ;// _display.asm,输出结果
     2 INCLUDE Irvine32.inc
     3 
     4 .code
     5 DisplaySum PROC
     6 theSum      EQU [ebp+12]
     7 ptrPrompt   EQU [ebp+8]
     8     enter   0, 0
     9     push    eax
    10     push    edx
    11 
    12     mov     edx, ptrPrompt
    13     call    WriteString
    14     mov     eax, theSum
    15     call    WriteInt
    16     call    Crlf
    17 
    18     pop     edx
    19     pop     eax
    20     leave
    21     ret     8
    22 DisplaySum ENDP
    23 END

    ● 代码,使用伪指令 PROTO 和 INVOKE 传参

     1 ;// Sum_main.asm,主过程
     2 INCLUDE sum.inc                                                 ; 需要头文件
     3 
     4 Count = 3
     5 
     6 .data
     7 prompt1 BYTE  "Enter a signed 32-bit integer: ", 0
     8 prompt2 BYTE  "Sum: ", 0
     9 array   DWORD  Count DUP(?)
    10 sum     DWORD  ?
    11 
    12 .code
    13 main PROC
    14     call Clrscr
    15 
    16     INVOKE PromptForIntegers, ADDR prompt1, ADDR array, Count   ; 3 个过程直接传参
    17 
    18     INVOKE ArraySum, ADDR array, Count
    19     mov     sum, eax
    20 
    21     INVOKE DisplaySum, ADDR prompt2, sum
    22 
    23     call    Crlf
    24     call    WaitMsg
    25     exit
    26 main ENDP
    27 END main
     1 ;// _prompt.asm,提示信息和输入整数
     2 INCLUDE sum.inc
     3 
     4 .code
     5 PromptForIntegers PROC, ptrPrompt:PTR BYTE, ptrArray:PTR DWORD, arraySize:DWORD
     6     pushad                  ; 不用 enter 和 leave
     7 
     8     mov     ecx, arraySize
     9     cmp     ecx, 0
    10     jle     L2
    11     mov     edx, ptrPrompt
    12     mov     esi, ptrArray
    13 
    14 L1:
    15     call    WriteString
    16     call    ReadInt    
    17     call    Crlf       
    18     mov     [esi], eax  
    19     add     esi, 4      
    20     loop    L1
    21 
    22 L2:    
    23     popad
    24     ret
    25 PromptForIntegers ENDP
    26 END
     1 ;// _arrysum.asm,数组求和
     2 INCLUDE sum.inc
     3 .code
     4 
     5 ArraySum PROC, ptrArray:PTR DWORD, arraySize:DWORD
     6     push    ecx
     7     push    esi
     8 
     9     mov     eax, 0
    10     mov     esi, ptrArray
    11     mov     ecx, arraySize
    12     cmp     ecx, 0
    13     jle     L2
    14 
    15 L1:
    16     add     eax, [esi]
    17     add     esi, 4
    18     loop    L1
    19 
    20 L2:
    21     pop     esi
    22     pop     ecx
    23     ret
    24 ArraySum ENDP
    25 END
     1 ;// _display.asm,输出结果
     2 INCLUDE Sum.inc
     3 
     4 .code
     5 DisplaySum PROC, ptrPrompt:PTR BYTE, theSum:DWORD
     6     push    eax
     7     push    edx
     8 
     9     mov     edx, ptrPrompt
    10     call    WriteString
    11     mov     eax, theSum
    12     call    WriteInt
    13     call    Crlf
    14 
    15     pop     edx
    16     pop     eax
    17     ret
    18 DisplaySum ENDP
    19 END
    1 ;// sum.inc,头文件
    2 INCLUDE Irvine32.inc
    3 
    4 PromptForIntegers PROTO, ptrPrompt:PTR BYTE, ptrArray:PTR DWORD, arraySize:DWORD
    5 
    6 ArraySum PROTO, ptrArray:PTR DWORD, count:DWORD
    7 
    8 DisplaySum PROTO, ptrPrompt:PTR BYTE, theSum:DWORD
  • 相关阅读:
    JS 使用 html2canvas 将页面转PDF
    代码记录
    js 原生图片批量上传 multiple="multiple"
    Go中的HTTP debug技能
    位运算 交换2个数 异或运算
    记一次 JMeter 压测 HTTPS 性能问题 拂衣 阿里云云栖号 20220527 19:00 发表于北京
    RVA relative virtual address PE
    进制
    浏览器是不允许页面直接读取本地硬盘资源的
    四平方和定理
  • 原文地址:https://www.cnblogs.com/cuancuancuanhao/p/9613863.html
Copyright © 2020-2023  润新知