如何访问每个单词大会? [英] How do I access each word in Assembly?

查看:114
本文介绍了如何访问每个单词大会?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设:

 。数据
ARR:.word 2,5,1,3,4
LEN:.word 5
总之:.word 0

我将如何访问在改编,比如2,3和4的每个字?

最后,我愿做的是找到所有的改编的值的总和,但我有困难,通过改编进行迭代。

感谢您的时间!

附加信息:


  1. 我使用eduMIPS64


解决方案

首先加载阵列成寄存器的地址,然后就可以进行不断弥补访问的项目。 (您汇编器可以支持的结构,如 LW $ T0,编曲+ 12 作为方便的简写本。请参阅手册。)对于迭代,要么递增地址寄存器,或添加另一个寄存器含有的偏移。一定要考虑项目的大小。 Folling例子是32位MIPS,调整为64位的必要的:

 。数据
ARR:.word 2,5,1,3,4
LEN:.word 5
总之:.word 0。文本
.globl主
主要:
    LA $ T0,编曲
    LW $ T1,12($ T0)#负载ARR [3]用字节偏移
    李$ T1,3#指数
    SLL $ T1,T1 $,2#乘以项目的大小
    阿杜$ T1,T1 $,$#T0添加基址
    LW $ T1,($ T1)#负载ARR [3]#迭代式1
#为(I = LEN,PTR =改编; I = 0;!我 - = 1,PTR + = 1)
#...使用* PTR ...
    LA $ T0,编曲
    LW $ T1,LEN#负载长度
循环1:
    LW $ T2,($ T0)#负载项
    在此过程中#项目
    阿迪$ T0,T0 $,4#新增项目大小的指针
    阿迪$ T1,T1 $,-1#递减计数器
    BNE $ T1,$ 0,循环1#如果计数器!= 0,重复#迭代2型
#为(!I = 0,I = LEN; I + = 1)
#...使用ARR [I] ...
    LA $ T0,编曲
    LW $ T1,LEN#负载长度
    李$ T2,0#指数
循环2:
    SLL $ T3,T2 $,2#乘以项目的大小
    阿杜$ T3,T3 $,$#T0添加基址
    LW $ T3,($ T3)#负载项
    在此过程中#项目
    阿迪$ T2,T2 $ 1#增量指标
    $ BNE T2,T1 $,循环2#如果index!= LEN,重复

(注意,这些样品环不处理零长度的数组,如果需要添加校验)

Given:

.data
arr: .word 2,5,1,3,4
len: .word 5
sum: .word 0

How would I access each word in "arr" such as 2, 3 and 4?

Eventually, what I would like to do is find a sum of all of the values in "arr", but I'm having difficulty iterating through "arr".

Thank you for your time!

Additional Info:

  1. I'm using eduMIPS64

解决方案

First load the address of the array into a register, then you can access the items with a constant offset. (Your assembler might support constructs such as lw $t0, arr+12 as convenience shorthand for this. See your manual.) For iteration, either increment the address register, or add another register containing the offset. Be sure to account for item sizes. Folling example is for 32 bit mips, adjust as necessary for 64 bit:

.data
arr: .word 2,5,1,3,4
len: .word 5
sum: .word 0

.text
.globl main
main:
    la $t0, arr
    lw $t1, 12($t0)     # load arr[3] using byte offset
    li $t1, 3           # index
    sll $t1, $t1, 2     # multiply by item size
    addu $t1, $t1, $t0  # add base address
    lw $t1, ($t1)       # load arr[3]

# iteration type 1
# for(i=len, ptr=arr; i != 0; i -= 1, ptr += 1)
# ... use *ptr ...
    la $t0, arr
    lw $t1, len         # load length
loop1:
    lw $t2, ($t0)       # load item
    # process item here
    addi $t0, $t0, 4    # add item size to pointer
    addi $t1, $t1, -1   # decrement counter
    bne $t1, $0, loop1  # if counter != 0, repeat

# iteration type 2
# for(i=0, i != len; i += 1)
# ... use arr[i] ...
    la $t0, arr
    lw $t1, len         # load length
    li $t2, 0           # index
loop2:
    sll $t3, $t2, 2     # multiply by item size
    addu $t3, $t3, $t0  # add base address
    lw $t3, ($t3)       # load item
    # process item here
    addi $t2, $t2, 1    # increment index
    bne $t2, $t1, loop2 # if index != len, repeat

(note these sample loops do not handle zero length array, add check if necessary)

这篇关于如何访问每个单词大会?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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