AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题 / 79125940
Accepted
Fatima sami
Fatima sami
Asked: 2024-10-25 21:36:53 +0800 CST2024-10-25 21:36:53 +0800 CST 2024-10-25 21:36:53 +0800 CST

如何在子程序中将结果推送到堆栈上?

  • 772
org 0x0100      
jmp start  

; Declare variables
input1: db 0  
input2: db 7  
input3: db 9  
input4: db 1  
message1: db 'The value of AX, BX and CX is: '
length: dw 31

start:
    ; Push output variables with random values
    push 3       ; Random value 1
    push 5       ; Random value 2
    push 7       ; Random value 3

    call my_subroutine

    ; Retrieve output variables
    pop cx       ; Retrieve output into CX
    pop bx       ; Retrieve output into BX
    pop ax       ; Retrieve output into AX

    ;Display the output values
    call display
    
    ; Exit program
    mov ax, 0x4c00
    int 0x21

my_subroutine:
    ; Save registers
    push ax
    push bx
    push si
    push di
    push cx

    ; Local variables (initialized with random values)
    local1: dw 1
    local2: dw 2
    local3: dw 3
    local4: dw 4
    local5: dw 5

    ; Example operations (just random logic for demonstration)
    mov ax, [input1] ; Load first input
    add ax, [local1] ; Add local variable
    mov bx, [input2] ; Load second input
    sub bx, [local2] ; Subtract local variable

    ; Push results
    ;push ax          ; Push first output (AX)
    ;push bx          ; Push second output (BX)
    ;mov cx, word [input3]      
    ;push cx          ; Push third output (local variable)

    ; Restore registers
    pop cx           ; Pop last pushed value into CX
    pop bx           ; Restore BX
    pop ax           ; Restore AX
    pop di           ; Restore DI
    pop si           ; Restore SI
    ret              ; Return to the caller

cls:

  push es
  push ax
  push di

  mov ax, 0xB800
  mov es, ax
  mov di, 0

 allSpace:

 mov word [es:di], 0x0720
 add di, 2
 cmp di, 4000
 jne allSpace

 pop di
 pop ax
 pop es

 ret

convert_num_to_string:
    push bp
    mov bp, sp 
    push es
    pusha
    mov ax, 0xb800
    mov es, ax
    mov ax, [bp+4]
    mov bx, 10
    mov cx, 0

 nextdigit: 
    mov dx, 0
    div bx
    add dl, 0x30
    push dx
    inc cx
    cmp ax, 0
    jnz nextdigit


    nextpos: 
    pop dx
    mov dh, 0x07
    mov [es:di], dx
    add di, 2
    loop nextpos

    popa
    pop es
    pop bp
    ret 2

display_message:
  push bp
  mov bp, sp
  push es
  push ax
  push bx
  push cx

  mov ax, 0xb800
  mov es, ax

  mov si, [bp+6]
  mov cx, [bp+4]
  mov ah, 0x07

  printAllChar:
  
  mov al, [si]
  mov [es:di], ax
  add di, 2
  add si, 1
  loop printAllChar

  pop cx
  pop bx
  pop ax
  pop es
  pop bp
  ret 4

display:  
    call cls
    mov di, 0                  
    push message1                  
    push word [length]
    call display_message
    
    mov di, 62 
    push ax           
    call convert_num_to_string

    mov di, 66
    push bx           
    call convert_num_to_string

    mov di, 70
    push cx           
    call convert_num_to_string


    ret



主要问题my_subroutine:

在上面的子程序中,当我将结果推送到堆栈时,控制不会返回开始,而是运行无限循环

顺便说一下,这是我试图解决的问题:

您需要用汇编语言编写一个子程序:

  1. 接受 4 个输入变量(即您的学号中的数字,即 22F-3440,3440 是 4 个输入)。
  2. 返回 3 个输出变量。这些输出变量(使用随机值)被推送到主函数中的输入变量之前。
  3. 在函数内部创建 5 个局部变量。使用 1 到 10 之间的随机值进行初始化。
  4. 使用寄存器 AX、BX、SI、DI 和 CX 执行运算,但在使用前后保存和恢复这些寄存器,以避免覆盖其原始值。
  5. 子程序返回后,必须将输出弹出到 AX、BX 和 CX 中。
  6. 将这些显示到控制台。
assembly
  • 1 1 个回答
  • 48 Views

1 个回答

  • Voted
  1. Best Answer
    Sep Roland
    2024-10-27T01:52:46+08:002024-10-27T01:52:46+08:00
    ; Local variables (initialized with random values)
    local1: dw 1
    local2: dw 2
    local3: dw 3
    local4: dw 4
    local5: dw 5
    

    这些不是局部变量!它们和您声明的其他变量( input、message、length )一样是全局变量。它们看起来只是局部的,因为它们的名称是这样的,并且因为您将它们写在了my_subroutine子例程内。在许多汇编程序中,您至少可以通过在这些变量的名称前面加上一个点:.local1: dw 1,使这些变量“仅在本地可访问”。但这仍然不是理想的选择,因为只要您的程序运行,它们就会继续占用内存。
    此外,由于它们位于程序的执行路径中,CPU 将执行这些数据字节,可能会导致灾难性的后果。在您的主函数中,您知道必须使用那个来跳过数据声明jmp start。所以,您可以在这里使用类似的东西。
    无论如何,对于您的任务,最好的解决方案是将局部变量放在堆栈上。请参阅下面的代码示例。

    ; Save registers
    push ax
    push bx
    push si
    push di
    push cx
    
    ...
    
    ; Restore registers
    pop cx
    pop bx
    pop ax
    pop di
    pop si
    ret
    

    您必须按照与之前推送寄存器相反的顺序恢复寄存器。除了 CX,这里不是这种情况!

    1. 返回 3 个输出变量。这些输出变量(使用随机值)被推送到主函数中的输入变量之前。

    这句话很重要,我的看法是,在调用子程序之前,总共有7 个值被推送到堆栈上。由于与局部变量不同,随机值没有限制,因此您可以节省几个字节,只需推送 AX、BX 和 CX 寄存器中碰巧存在的任何(随机)值即可。或者,只需考虑堆栈内存已经充满了这样的“随机”值,因此只需sub sp, 6为这 3 个输出保留空间:

      ; Push output variables with random values
      push cx      ; (1)                 alternative:   sub sp, 6    ; (1)(2)(3)
      push bx      ; (2)
      push ax      ; (3)
      ; Push input variables with digits from roll number
      push 1       ; 4th digit
      push 9       ; 3rd digit
      push 7       ; 2nd digit
      push 0       ; 1st digit
      call my_subroutine
      pop  ax      ; (3)
      pop  bx      ; (2)
      pop  cx      ; (1)
    

    进入子程序后的堆栈布局:

    1  2  3  4  5  CX DI SI BX AX BP RET 0  7  9  1  ?  ?  ?
    ^  ^                          ^      ^  ^        ^  ^  ^
    |  [bp-18]                    [bp]   |  [bp+6]   |  |  [bp+16]
    [bp-20]                              [bp+4]      |  [bp+14]
                                                     [bp+12]
    
    my_subroutine:
      push bp
      mov  bp, sp
      push ax      ; Save registers
      push bx
      push si
      push di
      push cx
      push 5       ; Local variables (initialized with random values)
      push 4
      push 3
      push 2
      push 1
    
      ; Example operations (just random logic for demonstration)
      mov  ax, [bp+4]    ; Load first input
      add  ax, [bp-20]   ; Add local variable
      mov  bx, [bp+6]    ; Load second input
      sub  bx, [bp-18]   ; Subtract local variable
      ...
    
    
      mov  [bp+12], ax   ; "Push" results
      mov  [bp+14], bx
      mov  [bp+16], cx
      add  sp, 10        ; Release local variables
      pop  cx            ; Restore registers
      pop  di
      pop  si
      pop  bx
      pop  ax
      pop  bp
      ret  8             ; Remove the 4 inputs
    

    display_message当前忘记保存 SI 和 DI,但毫无必要地保存了 BX。此外,将 DI 与几个堆栈输入一起视为此例程的寄存器输入有点奇怪。我认为 3 个堆栈输入或 3 个寄存器输入会更好...

    • 3

相关问题

  • x86 - 通过 RETF 从 32 位切换到 64 位

  • 小 ESP32-C3(基于 RISC-V)的浮点汇编指令上无法识别的操作码

  • 如何使用GNU Assembler (GAS)从相应的.s文件创建手写的ELF文件

  • 我无法读取gameboy的LY寄存器

  • 获取Assembly中的参数值

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    为什么这个简单而小的 Java 代码在所有 Graal JVM 上的运行速度都快 30 倍,但在任何 Oracle JVM 上却不行?

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    何时应使用 std::inplace_vector 而不是 std::vector?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Marko Smith

    我正在尝试仅使用海龟随机和数学模块来制作吃豆人游戏

    • 1 个回答
  • Martin Hope
    Aleksandr Dubinsky 为什么 InetAddress 上的 switch 模式匹配会失败,并出现“未涵盖所有可能的输入值”? 2024-12-23 06:56:21 +0800 CST
  • Martin Hope
    Phillip Borge 为什么这个简单而小的 Java 代码在所有 Graal JVM 上的运行速度都快 30 倍,但在任何 Oracle JVM 上却不行? 2024-12-12 20:46:46 +0800 CST
  • Martin Hope
    Oodini 具有指定基础类型但没有枚举器的“枚举类”的用途是什么? 2024-12-12 06:27:11 +0800 CST
  • Martin Hope
    sleeptightAnsiC `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它? 2024-11-09 07:18:53 +0800 CST
  • Martin Hope
    The Mad Gamer 何时应使用 std::inplace_vector 而不是 std::vector? 2024-10-29 23:01:00 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST
  • Martin Hope
    MarkB 为什么 GCC 生成有条件执行 SIMD 实现的代码? 2024-02-17 06:17:14 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve