|
| 1 | +# load |
| 2 | + |
| 3 | +Demonstrates how to load data from a combination of pointers and offsets with `avo`. |
| 4 | + |
| 5 | +The [code generator](asm.go) is as follows: |
| 6 | + |
| 7 | +## Read from a []byte |
| 8 | + |
| 9 | +This function takes a slice of bytes and an offset and returns the byte at that |
| 10 | +offset from the start of the slice. You can use an instance of the |
| 11 | +`operand.Mem` struct to create a pointer to a memory location. Note that the |
| 12 | +`Base` is a register containing the address of the slice `b`'s data and the |
| 13 | +`Index` is a register containing the offset value. We indicate the `Scale` here |
| 14 | +to calculate the full size-in-bytes of the offset, since the data here is |
| 15 | +single bytes the `Scale` is 1. Once this is done we use `MOVB` to read from the |
| 16 | +memory location into an 8 bit register. We use `Store(...)` and |
| 17 | +`ReturnIndex(0)` to write the return value. |
| 18 | + |
| 19 | +[embedmd]:# (asm.go go /.*TEXT.*ByteFromSlice/ /RET.*/) |
| 20 | +```go |
| 21 | + TEXT("ByteFromSlice", NOSPLIT, "func(b []byte, offset int) byte") |
| 22 | + bytesBase := Load(Param("b").Base(), GP64()) |
| 23 | + bytesOffset := Load(Param("offset"), GP64()) |
| 24 | + |
| 25 | + bytesData := Mem{Base: bytesBase, Index: bytesOffset, Scale: 1} |
| 26 | + bytesByte := GP8() |
| 27 | + MOVB(bytesData, bytesByte) |
| 28 | + |
| 29 | + Store(bytesByte, ReturnIndex(0)) |
| 30 | + RET() |
| 31 | +``` |
| 32 | + |
| 33 | +[embedmd]:# (mem.s) |
| 34 | +```s |
| 35 | +// func ByteFromSlice(b []byte, offset int) byte |
| 36 | +TEXT ·ByteFromSlice(SB), NOSPLIT, $0-33 |
| 37 | + MOVQ b_base+0(FP), AX |
| 38 | + MOVQ offset+24(FP), CX |
| 39 | + MOVB (AX)(CX*1), AL |
| 40 | + MOVB AL, ret+32(FP) |
| 41 | + RET |
| 42 | +``` |
| 43 | + |
| 44 | +## Read from a string |
| 45 | + |
| 46 | +This function takes a string and an offset and returns the byte at that offset |
| 47 | +from the start of the string. We use an instance of `operand.Mem` just as |
| 48 | +above. Because the string is interpreted as being made of bytes here the |
| 49 | +`Scale` is still 1. |
| 50 | + |
| 51 | +[embedmd]:# (asm.go go /.*TEXT.*ByteFromSlice/ /RET.*/) |
| 52 | +```go |
| 53 | + TEXT("ByteFromString", NOSPLIT, "func(s string, offset int) byte") |
| 54 | + stringBase := Load(Param("s").Base(), GP64()) |
| 55 | + stringOffset := Load(Param("offset"), GP64()) |
| 56 | + |
| 57 | + stringData := Mem{Base: stringBase, Index: stringOffset, Scale: 1} |
| 58 | + stringByte := GP8() |
| 59 | + MOVB(stringData, stringByte) |
| 60 | + |
| 61 | + Store(stringByte, ReturnIndex(0)) |
| 62 | + RET() |
| 63 | +``` |
| 64 | + |
| 65 | +[embedmd]:# (mem.s) |
| 66 | +```s |
| 67 | +// func ByteFromString(s string, offset int) byte |
| 68 | +TEXT ·ByteFromString(SB), NOSPLIT, $0-25 |
| 69 | + MOVQ s_base+0(FP), AX |
| 70 | + MOVQ offset+16(FP), CX |
| 71 | + MOVB (AX)(CX*1), AL |
| 72 | + MOVB AL, ret+24(FP) |
| 73 | + RET |
| 74 | +``` |
| 75 | + |
| 76 | +## Read from an int32 slice |
| 77 | + |
| 78 | +This function takes a slice of int32 and an offset and returns the int32 at |
| 79 | +that offset from the start of the slice. We use an instance of `operand.Mem` |
| 80 | +just as above. Because the slice contains int32 sized data the `Scale` is 4, as |
| 81 | +each int32 is 4 bytes wide. |
| 82 | + |
| 83 | +[embedmd]:# (asm.go go /.*TEXT.*ByteFromSlice/ /RET.*/) |
| 84 | +```go |
| 85 | + TEXT("Int32FromSlice", NOSPLIT, "func(i []int32, offset int) int32") |
| 86 | + int32Base := Load(Param("i").Base(), GP64()) |
| 87 | + int32Offset := Load(Param("offset"), GP64()) |
| 88 | + |
| 89 | + int32Data := Mem{Base: int32Base, Index: int32Offset, Scale: 4} |
| 90 | + int32Byte := GP32() |
| 91 | + MOVL(int32Data, int32Byte) |
| 92 | + |
| 93 | + Store(int32Byte, ReturnIndex(0)) |
| 94 | + RET() |
| 95 | +``` |
| 96 | + |
| 97 | +[embedmd]:# (mem.s) |
| 98 | +```s |
| 99 | +// func Int32FromSlice(i []int32, offset int) int32 |
| 100 | +TEXT ·Int32FromSlice(SB), NOSPLIT, $0-36 |
| 101 | + MOVQ i_base+0(FP), AX |
| 102 | + MOVQ offset+24(FP), CX |
| 103 | + MOVL (AX)(CX*4), AX |
| 104 | + MOVL AX, ret+32(FP) |
| 105 | + RET |
| 106 | +``` |
| 107 | + |
| 108 | +## Read from an int64 slice |
| 109 | + |
| 110 | +This function takes a slice of int64 and an offset and returns the int64 at |
| 111 | +that offset from the start of the slice. We use an instance of `operand.Mem` |
| 112 | +just as above. Because the slice contains int64 sized data the `Scale` is 8, as |
| 113 | +each int64 is 8 bytes wide. |
| 114 | + |
| 115 | +[embedmd]:# (asm.go go /.*TEXT.*ByteFromSlice/ /RET.*/) |
| 116 | +```go |
| 117 | + TEXT("Int64FromSlice", NOSPLIT, "func(i []int64, offset int) int64") |
| 118 | + int64Base := Load(Param("i").Base(), GP64()) |
| 119 | + int64Offset := Load(Param("offset"), GP64()) |
| 120 | + |
| 121 | + int64Data := Mem{Base: int64Base, Index: int64Offset, Scale: 4} |
| 122 | + int64Byte := GP64() |
| 123 | + MOVQ(int64Data, int64Byte) |
| 124 | + |
| 125 | + Store(int64Byte, ReturnIndex(0)) |
| 126 | + RET() |
| 127 | +``` |
| 128 | + |
| 129 | +[embedmd]:# (mem.s) |
| 130 | +```s |
| 131 | +// func Int64FromSlice(i []int64, offset int) int64 |
| 132 | +TEXT ·Int64FromSlice(SB), NOSPLIT, $0-40 |
| 133 | + MOVQ i_base+0(FP), AX |
| 134 | + MOVQ offset+24(FP), CX |
| 135 | + MOVQ (AX)(CX*4), AX |
| 136 | + MOVQ AX, ret+32(FP) |
| 137 | + RET |
| 138 | +``` |
0 commit comments