题
大家好,可以告诉我如何使用8086汇编语言处理2D阵列。我是汇编语言编程的初学者。谢谢
解决方案
Madhur的链接几乎涵盖了它,您读过了吗?
例如,如果您已经在C编程级别上了解了一个2D数组,则汇编器是下一个逻辑步骤。
使用8位字节,例如,如果您想这样考虑,则使用阵列z [1] [2]是第二行第三项,以计算它的地址,就像z地址中的c地址加上第一个索引时间数组的宽度,假设它是13个字节宽,以及第二个索引SO&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
蛮力将始终适用于每个平台,蛮力为基础地址 +(宽度时间第一索引) +(元素的第二个索引时间大小)。优化在很大程度上依赖您要做的事情,在第一个组装示例中,例如,如果第一个索引是硬编码的,则是愚蠢的,如果是硬编码,则将#2移至寄存器(如果是)一个硬编码的数字,只需添加2。如果计算一次与循环更改最佳使用寄存器数量,等等。如果您的平台没有倍数或痛苦很痛苦,那么如果可能想法,如果您不能更改宽度并且平台没有或使倍增倍增,则摆脱倍数或其他技巧以脱离。
具有某种寄存器偏移量的平台,地址为[R0,R1],其中地址是两个寄存器的总和,例如为您节省添加,并防止破坏基础地址寄存器,以便您可以在循环中再次使用它。如果您想在指针(*ptr ++)时使用销毁的指针样式,这可能会改变您实现循环的方式,某些平台允许您使用基本寄存器并为其添加值,例如[R0], #16将使用R0上的地址,然后在使用R0添加16到R0之后,因此您不必刻录额外的添加指令...我认为X86没有该指令,但是它具有可以用于此任务的其他功能。
从蛮力开始,成为x86,这意味着您可能必须使用内存来保持循环变量,因为您可能没有足够的寄存器来完成该任务(这是可以的(因为X86具有许多基于内存的指令)负载和存储变化的优势。
其他提示
;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;