arm64汇编学习

阅读: 评论:0

arm64汇编学习

arm64汇编学习

arm64汇编学习-(3)算术与移位指令

  • 1 数据处理指令
    • 1.1 check the C condition of adds, adc,cmp
      • 1.1.1 测试示例程序
      • 1.1.2 执行之前
      • 1.1.3 执行之后
        • 1.1.3.1 ldr和mov指令之后
        • 1.1.3.2 ads和adc指令之后
        • 1.1.3.3 cmp和adc指令之后
    • 1.2 cmp和sbc指令的综合运用
      • 1.2.1 示例代码
      • 1.2.2 compare_and_return(10, 9)测试之前
      • 1.2.3 compare_and_return(10, 9)测试之后
      • 1.2.4 compare_and_return(9, 10)测试之前
      • 1.2.5 compare_and_return(9, 10)测试之后
    • 1.3 测试ands指令对Z标志位的影响
      • 1.3.2 测试之前
      • 1.3.3 测试之后
    • 1.4 测试位段bitfield指令
      • 1.4.1 示例代码
      • 1.4.2 测试之前
      • 1.4.3 bfi x0, x1, 8, 4之后
      • 1.4.4 ubfx and sbfx之后
    • 1.5 读取寄存器的域
      • 1.5.1 示例代码

本篇博客是基于对苯叔的第三季视频的学习整理而得,大家如果想深入学习可以购买《arm64体系结构编程与实践》以及购买苯叔出品的第三季视频。

1 数据处理指令

1.1 check the C condition of adds, adc,cmp

1.1.1 测试示例程序

/*测试adds, cmp以及adc指令的条件标志位
*/
.global add_inst_test
add_inst_test:mov x0, #0ldr x1, =0xffffffffffffffffmov x2, #3/*测试adds的进位功能,当有溢出发生时,C=1*/adds x0, x1, x1adc x3, xzr, xzr/*测试cmp的条件标志位,当x1 > x2时,C=1当x1 < x2是, C=0*/cmp x1, x2adc x4, xzr, xzrret

1.1.2 执行之前

1.1.3 执行之后

1.1.3.1 ldr和mov指令之后

1.1.3.2 ads和adc指令之后

ADDS: Add (extended register), setting flags, adds a register value and a sign or zero-extended register value, followed by an optional left shift amount, and writes the result to the destination register. The argument that is extended from the register can be a byte, halfword, word, or doubleword. It updates the condition flags based on the result.

  • ADDS , <Xn|SP>, {, {#}}
  • (result, nzcv) = AddWithCarry(operand1, operand2, ‘0’);
  • PSTATE.<N,Z,C,V> = nzcv;
  • X[d] = result;
    ADC:Add with Carry adds two register values and the Carry flag value, and writes the result to the destination register.
  • (result, -) = AddWithCarry(operand1, operand2, PSTATE.C);
  • X[d] = result;
1.1.3.3 cmp和adc指令之后

CMP:This instruction is an alias of the SUBS (shifted register) instruction.

  • The encodings in this description are named to match the encodings of SUBS (shifted register).
  • The description of SUBS (shifted register) gives the operational pseudocode for this instruction.

1.2 cmp和sbc指令的综合运用

1.2.1 示例代码

ret = compare_and_return(10, 9);
val = compare_and_return(9, 10);/*当arg1 >= arg2 时, 返回 0当arg1 < arg2 时,返回0xffffffffffffffff
*/
.global compare_and_return
compare_and_return:cmp x0, x1sbc x0, xzr, xzrret

1.2.2 compare_and_return(10, 9)测试之前

1.2.3 compare_and_return(10, 9)测试之后

1.2.4 compare_and_return(9, 10)测试之前

1.2.5 compare_and_return(9, 10)测试之后

1.3 测试ands指令对Z标志位的影响

2.1.3.1 示例代码

/*data_process_instr lab3: 测试ands指令对Z标志位的影响
*/
.global ands_test
ands_test:mov x1, #0x3mov x2, #0//mov x2, #2ands x3, x1, x2/*读取NZCV寄存器,查看Z标志位是否为1, 见armv8.6手册第C5.2.9章*/mrs x0, nzcvret

1.3.2 测试之前

1.3.3 测试之后

1.4 测试位段bitfield指令

1.4.1 示例代码

/*data_process_instr lab4: 测试位段bitfield指令*/
.global bitfield_test
bitfield_test:/* 位段插入*/mov x1, 0x345mov x0, 0bfi x0, x1, 8, 4/*无符号数的bitfield提取,提取x2,从第4个bit开始,提取8位,x3其他位都是0,最终:0xbc*/ldr x2, =0x5678abcdubfx x3, x2, #4, #8/*有符号数的bitfield提取,提取x2,从第4个bit开始,提取8bit。x4的其他比特位都是f, 最终:0xffffffffffffffbc*/sbfx x4, x2, #4, #8ret

1.4.2 测试之前

1.4.3 bfi x0, x1, 8, 4之后

BFI :Bitfield Insert copies a bitfield of bits from the least significant bits of the source register to bit position of the destination register, leaving the other destination bits unchanged.

  • bfi x0, x1, 8, 4 该汇编指令的含义为:将x1寄存器的最低4位拷贝到x0寄存器的bit 8开始的4位上去,x0寄存器的其他位不变,所以最终x0寄存器的值被设置为0x500

1.4.4 ubfx and sbfx之后

UBFX :Unsigned Bitfield Extract copies a bitfield of bits starting from bit position in the source register to the least significant bits of the destination register, and sets destination bits above the bitfield to zero.

  • UBFX , , #, #
    SBFX :Signed Bitfield Extract copies a bitfield of bits starting from bit position in the source register to the least significant bits of the destination register, and sets destination bits above the bitfield to a copy of the most significant bit of the bitfield.
  • SBFX , , #, #
    This content is only supported in a Docs.

1.5 读取寄存器的域

1.5.1 示例代码

/*data_process_instr lab4: 测试位段bitfield指令*/
.global bitfield_test
bitfield_test:/* 位段插入*/mov x1, 0x345mov x0, 0bfi x0, x1, 8, 4/*无符号数的bitfield提取,提取x2,从第4个bit开始,提取8位,x3其他位都是0,最终:0xbc*/ldr x2, =0x5678abcdubfx x3, x2, #4, #8/*有符号数的bitfield提取,提取x2,从第4个bit开始,提取8bit。x4的其他比特位都是f, 最终:0xffffffffffffffbc*/sbfx x4, x2, #4, #8/*lab5: 使用ubfx指令来读取 寄存器的位域*/mrs x1, ID_AA64ISAR0_EL1/*读取atomic域的值:判断是否支持LSE指令*/ubfx x0, x1, #20, #4/*读取AES域的值:判断是否支持AES指令*/ubfx x2, x1, #4, #4ret

2.5.1.2 测试之前的状态

2.5.1.3 测试之后的状态值

本文发布于:2024-02-01 17:45:09,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170678154838392.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23