From 22cbb583b1b322490c959d3557d13d60bbdcada4 Mon Sep 17 00:00:00 2001 From: Perry Kivolowitz Date: Wed, 13 Jul 2022 12:41:26 -0500 Subject: [PATCH] added and documented example code --- section_2/bitfields/review.md | 41 +++++++++++++++++++++++++++++++++++ section_2/bitfields/ubfiz.s | 41 +++++++++++++++++++++++++---------- 2 files changed, 71 insertions(+), 11 deletions(-) diff --git a/section_2/bitfields/review.md b/section_2/bitfields/review.md index 8582fc3..eaf6a1c 100644 --- a/section_2/bitfields/review.md +++ b/section_2/bitfields/review.md @@ -117,6 +117,8 @@ specified bits from the source. The `u` means don't consider the sign bit as special. The `z` is what causes the zero fill first and sets it apart from `bfi`. +See bottom for an example. + ## `mvn` This instruction takes only takes two operands (but permits an optional @@ -147,6 +149,8 @@ That is, if your goal is to turn a positive integer into a negative integer, use `neg`. If your goal is to negate bits for bit bashing purposes, use `mvn`.* +See bottom for an example. + ## `lsl` **Logical Shift Left** moves bits to the left shifting 0 into any @@ -175,3 +179,40 @@ for bit bashing as it is how bits can be set to 1. orr rd, rs, rm # rm is another register orr rd, rs, imm ``` + +## Example + +[Here](./ubfiz.s) is the code to an example: + +```asm + .global main // 1 + .text // 2 + .align 2 // 3 +/* This demo should be run from gdb. `layout regs` would be helpful. // 4 + This demo shows: // 5 + bfi - bit field insert // 6 +*/ // 7 +main: mov w1, wzr // // 8 + mvn w1, w1 // set w1 to 0xFFFFFFFF // 9 + mov w3, w1 // save for reuse // 10 + mov w2, 3 // set bits 0 and 1 // 11 + bfi w1, w2, 4, 4 // 12 + // Look at w1. You will note that the bottom // 13 + // 4 bits of w2 (0011) have been copied into the second // 14 + // 4 bits of w1 including the zeros. // 15 + // // 16 + // NEXT DEMO // 17 + mov w1, w3 // reset w1 // 18 + mov w2, 3 // set bits 0 and 1 // 19 + ubfiz w1, w2, 4, 2 // 20 + // Look at w1. You will note that all the bits were // 21 + // zeroed and then 2 bits from w2 are copied into w1 // 22 + // starting at bit 4. Compare this to bfi. // 23 + // // 24 + // NEXT DEMO // 25 + bfxil w3, w1, 4, 4 // 26 + // Look at w3. You will note that 4 bits (0011) from // 27 + // w1 were put into w3 starting at bit 0. // 28 + mov w0, wzr // 29 + ret // 30 +``` diff --git a/section_2/bitfields/ubfiz.s b/section_2/bitfields/ubfiz.s index 41e2691..3088d00 100644 --- a/section_2/bitfields/ubfiz.s +++ b/section_2/bitfields/ubfiz.s @@ -1,11 +1,30 @@ - .global main - .text - .align 2 - -main: str x30, [sp, -16]! - mov w1, 0xFF - mov w2, 0x0A - ubfiz w1, w2, 0, 4 - ldr x30, [sp], 16 - mov w0, wzr - ret + .global main + .text + .align 2 +/* This demo should be run from gdb. `layout regs` would be helpful. + This demo shows: + bfi - bit field insert +*/ +main: mov w1, wzr // + mvn w1, w1 // set w1 to 0xFFFFFFFF + mov w3, w1 // save for reuse + mov w2, 3 // set bits 0 and 1 + bfi w1, w2, 4, 4 + // Look at w1. You will note that the bottom + // 4 bits of w2 (0011) have been copied into the second + // 4 bits of w1 including the zeros. + // + // NEXT DEMO + mov w1, w3 // reset w1 + mov w2, 3 // set bits 0 and 1 + ubfiz w1, w2, 4, 2 + // Look at w1. You will note that all the bits were + // zeroed and then 2 bits from w2 are copied into w1 + // starting at bit 4. Compare this to bfi. + // + // NEXT DEMO + bfxil w3, w1, 4, 4 + // Look at w3. You will note that 4 bits (0011) from + // w1 were put into w3 starting at bit 0. + mov w0, wzr + ret