首页 文章

汇编语言中的多维数组

提问于
浏览
3

嗨所有可以任何一个PLZ告诉我如何处理8086汇编语言中的2D阵列 . 我是汇编语言编程的初学者 . 谢谢

2 回答

  • 6
    ;This code grabs data bits from accumulator and outputs it to the carry
    Main:
    clr C
    mov A, #00101010b; 
    call array; 
    jmp Main; 
    array:
    subb A, #80H; mov can work here if you did not clr C
    cpl A; check for proper array location
    mov B, #8; set division factor
    DIV AB; bit offset contained in B; 
    mov R6,B;
    subb A, #30H; this bit will allow you to mov around the chosen register
    cpl A;   
    mov R7, A; 
    mov A, @R7; this is the array position
    inc R6; 
    loop: 
    rlc A; this takes the array bit to the carry for easy access
    djnz R6, loop;
    
  • -1

    Madhur的链接几乎涵盖了它,你读完了吗?

    如果你已经在C编程级别已经理解了2d数组,那么汇编程序就是下一个逻辑步骤 .

    使用8位字节,例如数组z [1] [2]是第二行第三项,如果你想这样想,计算它的地址,因为它在z的C地址加上第一个索引时间数组的宽度,假设它是13字节宽,再加上第二个索引,所以&z(13 * 1)2 =&z 15;

    使用伪代码而不是x86代码(如果这是作业) .

    ;brute force
    ldr r0,=z ;load address of z into r0
    mov r1,#13
    mul r1,#1 ;make this a register or load from a memory location
    mov r2,#2 ;make this a register or load from a memory location
    add r0,r1
    add r0,r2
    ldrb r1,[r0] ;read the byte
    strb r3,[r0] ;write the byte
    
    ;if your instruction set allows register offset
    ldr r0,=z ;load address of z into r0
    mov r1,#13
    mul r1,#1
    mov r2,#2
    add r1,r2
    ldrb r4,[r0,r1] ;read the byte
    strb r3,[r0,r1] ;write the byte
    
    ;or immediate offset and offset is hardcoded
    ldr r0,=z ;load address of z into r0
    mov r1,#13
    mul r1,#1
    add r0,r1
    ldrb r4,[r1,#2] ;read the byte
    strb r3,[r1,#2] ;write the byte
    

    如果你在C中有循环

    unsigned char x[4][16];
    unsigned char z[4][16];
    unsigned int ra,rb;
    
    for(ra=0;ra<4;ra++)
    {
      for(rb=0;rb<16;rb++)
      { 
          x[ra][rb]=z[ra][rb];
      }
    }
    

    转换为汇编程序非常简单 .

    ldr r0,=x
    ldr r1,=z
    mov r2,#0 ;ra
    outer:
      mov r3,#0 ;rb
      inner:
        mov r4,r2 lsl #2 ;16 bytes wide
        add r4,r3
        ldrb r5,[r1,r4]
        strb r5,[r0,r4]
        add r3,r3,#1
        cmp r3,#16
        bne inner
      add r2,r2,#1
      cmp r2,#4
      bne outer
    

    蛮力总是适用于每个平台,蛮力是基地址(宽度乘以第一个索引)(第二个索引乘以元素的大小) . 优化在很大程度上依赖于你要做的事情,在我做的第一个汇编示例中,如果第一个索引是硬编码的,那么将它乘以1是愚蠢的,和/或如果是,则将#2移动到寄存器是愚蠢的一个硬编码的数字,只需加2.如果计算一次对一个循环改变了要使用的最佳寄存器数量等等,如果你的平台没有乘法或者很痛苦那么如果可能的话,使你的阵列幂为2是好的想法,摆脱乘法,或其他技巧摆脱,如果你不能改变宽度,你的平台没有或使得倍增痛苦 .

    具有某种寄存器偏移量寻址[r0,r1]的平台,例如,地址是两个寄存器的总和,可以节省一个加法并防止破坏基址寄存器,以便您可以在循环中再次使用它 . 如果你想使用destroy指针样式去指针(* ptr),这可能会改变你实现循环的方式,某些平台允许你使用基址寄存器并为其添加一个值,例如[r0], #16将使用r0处的地址然后在使用r0后将16添加到r0,因此您不必刻录额外的添加指令...我不认为x86具有此功能,但它具有可用于此任务的其他功能 .

    从暴力开始,是x86,这意味着你可能不得不使用内存来保存循环变量,因为你可能没有足够的任务寄存器(这是可以的,因为x86有很多基于内存的指令),然后优化服务负载和存储变化的优势 .

相关问题