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 / 问题 / 79479684
Accepted
Pato
Pato
Asked: 2025-03-03 04:16:33 +0800 CST2025-03-03 04:16:33 +0800 CST 2025-03-03 04:16:33 +0800 CST

68000 汇编 - 查找数组中的最小值

  • 772

我正在用M68K汇编语言编写一个程序来查找数组中的最小值。

这是我的代码:

ORG $8000

; DATA
array DC.B 2,3,1,4,5  ; The minimum is 1
len EQU 5
min DC.L 0          ; Stores the found minimum

START:
     lea.l array,a0
     move.b (a0)+,d0   ; d0 = 2 (current minimum), a0 now points to 3
     moveq.l #len-1,d1 ; One element already used

LOOP: 
    cmpi.l #0,d1       ; If d1 = 0, no more elements to check
    beq.s END          ; Jump to the end if finished
    
    move.b (a0)+,d2    ; Load next element into d2
                       ; d2 = 3, a0 -> 1
                       ; d2 = 1, a0 -> 4
                       ; d2 = 4, a0 -> 5
                       ; d2 = 5, a0 -> ??? (What happens here?)
                       
    cmp.b d0,d2 
    blt.s UPDATE       ; If d2 < d0, update minimum

    bra.s DECREMENT

UPDATE:
     move.b d2,d0      ; Update the minimum

DECREMENT:
     subq.l #1,d1      ; Decrease counter
     bra.s LOOP

END: 
    move.b d0,min   ; Store the minimum
    SIMHALT
    END START

我的问题:

  1. 在我标记的位置,处理完最后一个元素5???后会发生什么?它是否指向无效地址?a0
  2. 我的初始化方法是否是d0一个move.b (a0)+,d0好的开始方式,或者是否有更好的方法?
  3. 我的代码还可以进一步优化吗?

任何建议都将不胜感激!谢谢!

assembly
  • 1 1 个回答
  • 54 Views

1 个回答

  • Voted
  1. Best Answer
    Sep Roland
    2025-03-03T04:57:00+08:002025-03-03T04:57:00+08:00
    1. 在我标记的位置,处理完最后一个元素5???后会发生什么?它是否指向无效地址?a0

    您的最小变量直接跟随数组,因此 A0 将指向最小长字变量的第一个字节。

    1. 我的初始化方法是否是d0一个move.b (a0)+,d0好的开始方式,或者是否有更好的方法?

    没关系。

    1. 我的代码还可以进一步优化吗?

    当然!

    • 无需在循环顶部测试零计数器。不过,您可以在循环之前测试它以防万一。关于进行更多迭代的决定应如下进行:

         subq.l   #1, d1      ; Decrease counter
         bne.s    LOOP
      
    • 尽量避免必须这样做BRA。如果 D2 < D0,则不分支到UPDATE,而是基于相反的条件绕过bge:

         cmp.b    d0, d2
         bge.s    SKIP       ; If d2 >= d0, skip updating the minimum
         move.b   d2, d0     ; Update the minimum
        SKIP:
      

    ORG $8000
    
    ; DATA
    array DC.B 2,3,1,4,5  ; The minimum is 1
    len   EQU 5
    min   DC.L 0          ; Stores the found minimum
    
    START:
         lea.l   array, a0
         move.b  (a0)+, d0   ; d0 = 2 (current minimum), a0 now points to 3
         moveq.l #len-1, d1  ; One element already used
         ; Could bail-out here if D1 is zero
    LOOP: 
        move.b   (a0)+, d2   ; Load next element into d2
        cmp.b    d0, d2 
        bge.s    SKIP        ; If d2 >= d0, skip updating the minimum
        move.b   d2, d0      ; Update the minimum
    SKIP:
        subq.l   #1, d1      ; Decrease counter
        bne.s    LOOP
    END: 
        move.b   d0, min     ; Store the minimum
        SIMHALT
        END START
    

    由于您一直在处理字节,因此您可以定义最小值一个字节。

    [编辑]

    Erik Eidt 注意到了写入长字变量错误部分的问题。

    • 由于min长字变量直接跟在 5 字节数组之后,而该数组本身从偶数地址 $8000 开始,因此min的地址将为奇数。68000不允许字或长字访问奇数地址。CPU 将为其生成“地址错误”异常。您可以通过在数组和变量之间插入填充字节或使用.EVEN指令(如果可用)来解决此问题。
    • 因为 68000 是大端机器,所以字和长字以最高有效字节在前(在最低地址)的方式存储在内存中。这意味着,如果您写入,move.b d0, min您将把值存储在权重最大的字节中。不可避免地,这会产生错误的结果。解决方案是存储一个完整的长字,并且由于您的数组应该包含有符号字节,因此使用ext指令可以解决这个问题:ext.w d0首先从字节符号扩展到字,然后ext.l d0从字符号扩展到长字。

    当然,如果最小变量是字节大小的话,这些额外的复杂性就不会存在。

    修改后的方案:

    ORG $8000
    
    ; DATA
    array DC.B 2,3,1,4,5  ; The minimum is 1
    len   EQU 5
          DC.B 0          ; Filler (You should look for the .EVEN directive)
    min   DC.L 0          ; Stores the found minimum
    
    START:
         lea.l   array, a0
         move.b  (a0)+, d0   ; d0 = 2 (current minimum), a0 now points to 3
         moveq.l #len-1, d1  ; One element already used
         ; Could bail-out here if D1 is zero
    LOOP: 
        move.b   (a0)+, d2   ; Load next element into d2
        cmp.b    d0, d2 
        bge.s    SKIP        ; If d2 >= d0, skip updating the minimum
        move.b   d2, d0      ; Update the minimum
    SKIP:
        subq.l   #1, d1      ; Decrease counter
        bne.s    LOOP
    END:
        ext.w    d0
        ext.l    d0
        move.l   d0, min     ; Store the minimum
        SIMHALT
        END START
    
    • 2

相关问题

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

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

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

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

  • 获取Assembly中的参数值

Sidebar

Stats

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

    重新格式化数字,在固定位置插入分隔符

    • 6 个回答
  • Marko Smith

    为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会?

    • 2 个回答
  • Marko Smith

    VScode 自动卸载扩展的问题(Material 主题)

    • 2 个回答
  • Marko Smith

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

    • 1 个回答
  • Marko Smith

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

    • 1 个回答
  • Marko Smith

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

    • 6 个回答
  • Marko Smith

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

    • 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 个回答
  • Martin Hope
    Fantastic Mr Fox msvc std::vector 实现中仅不接受可复制类型 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant 使用 chrono 查找下一个工作日 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor 构造函数的成员初始化程序可以包含另一个成员的初始化吗? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský 为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul C++20 是否进行了更改,允许从已知绑定数组“type(&)[N]”转换为未知绑定数组“type(&)[]”? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann 为什么 {2,3,10} 和 {x,3,10} (x=2) 的顺序不同? 2025-01-13 23:24:07 +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

热门标签

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