From adbcd27a439b53f1c07e855a938f1db56a8ab04f Mon Sep 17 00:00:00 2001 From: John Winans Date: Fri, 5 Mar 2021 13:53:45 -0600 Subject: [PATCH] Add leading zeros to literals to eliminate sign confusion --- book/programs/chapter.tex | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/book/programs/chapter.tex b/book/programs/chapter.tex index 36b2c49..aac2f5d 100644 --- a/book/programs/chapter.tex +++ b/book/programs/chapter.tex @@ -365,26 +365,26 @@ Introduce and present subroutines but not nesting until introduce stack operatio {\small \begin{verbatim} li rd,constant lui rd,(constant >>U 12)+(constant & 0x00000800 ? 1 : 0) - addi rd,rd,(constant & 0xfff) + addi rd,rd,(constant & 0x00000fff) la rd,label auipc rd,((label-.) >>U 12) + ((label-.) & 0x00000800 ? 1 : 0) - addi rd,rd,((label-(.-4)) & 0xfff) + addi rd,rd,((label-(.-4)) & 0x00000fff) l{b|h|w} rd,label auipc rd,((label-.) >>U 12) + ((label-.) & 0x00000800 ? 1 : 0) - l{b|h|w} rd,((label-(.-4)) & 0xfff)(rd) + l{b|h|w} rd,((label-(.-4)) & 0x00000fff)(rd) s{b|h|w} rd,label,rt # rt used as a temp reg for the operation (default=x6) auipc rt,((label-.) >>U 12) + ((label-.) & 0x00000800 ? 1 : 0) - s{b|h|w} rd,((label-(.-4)) & 0xfff)(rt) + s{b|h|w} rd,((label-(.-4)) & 0x00000fff)(rt) call label auipc x1,((label-.) >>U 12) + ((label-.) & 0x00000800 ? 1 : 0) - jalr x1,((label-(.-4)) & 0xfff)(x1) + jalr x1,((label-(.-4)) & 0x00000fff)(x1) tail label,rt # rt used as a temp reg for the operation (default=x6) auipc rt,((label-.) >>U 12) + ((label-.) & 0x00000800 ? 1 : 0) - jalr x0,((label-(.-4)) & 0xfff)(rt) + jalr x0,((label-(.-4)) & 0x00000fff)(rt) mv rd,rs addi rd,rs,0 @@ -427,7 +427,7 @@ To remedy this problem, the value used in the {\tt lui} instruction can altered instruction: {\small \begin{verbatim} - lui x5,0x12346 // x5 = 0x12346000 (note this is 0x12345 + 1) + lui x5,0x12346 // x5 = 0x12346000 (note: this is 0x12345000 + 0x1000) addi x5,x5,0x800 // x5 = 0x12346000 + sx(0x800) = 0x12346000 + 0xfffff800 = 0x12345800 \end{verbatim} } @@ -481,7 +481,7 @@ The \verb@la@ instruction here will expand into: {\small \begin{verbatim} 00010040 auipc x10,((var1-.) >>U 12) + ((var1-.) & 0x00000800 ? 1 : 0) -00010044 addi x10,x10,((var1-(.-4)) & 0xfff) +00010044 addi x10,x10,((var1-(.-4)) & 0x00000fff) \end{verbatim} } @@ -498,14 +498,14 @@ Therefore the expanded pseudoinstruction example will become: {\small \begin{verbatim} 00010040 auipc x10,((0x00010900-0x00010040) >>U 12) + ((0x00010900-0x00010040) & 0x00000800 ? 1 : 0) -00010044 addi x10,x10,((0x00010900-(0x00010044-4)) & 0xfff) # note the extra -4 here! +00010044 addi x10,x10,((0x00010900-(0x00010044-4)) & 0x00000fff) # note the extra -4 here! \end{verbatim} } After performing the subtractions, it will reduce to this: {\small \begin{verbatim} 00010040 auipc x10,(0x000008c0 >>U 12) + ((0x000008c0) & 0x00000800 ? 1 : 0) -00010044 addi x10,x10,(0x000008c0 & 0xfff) +00010044 addi x10,x10,(0x000008c0 & 0x00000fff) \end{verbatim} } Continuing to reduce the math operations we get: @@ -546,7 +546,7 @@ To refer to an absolute value, the following operators can be used: {\small \begin{verbatim} %hi(constant) // becomes: (constant >>U 12)+(constant & 0x00000800 ? 1 : 0) - %lo(constant) // becomes: (constant & 0xfff) + %lo(constant) // becomes: (constant & 0x00000fff) \end{verbatim} } @@ -567,7 +567,7 @@ The following can be used for PC-relative addresses: {\small \begin{verbatim} %pcrel_hi(symbol) // becomes: ((symbol-.) >>U 12) + ((symbol-.) & 0x00000800 ? 1 : 0) - %pcrel_lo(lab) // becomes: ((symbol-lab) & 0xfff) + %pcrel_lo(lab) // becomes: ((symbol-lab) & 0x00000fff) \end{verbatim} } @@ -590,7 +590,7 @@ Examples of using the \verb@auipc@ \& \verb@addi@ together with \verb@%pcrel_hi( {\small \begin{verbatim} xxx: auipc t1,%pcrel_hi(yyy) // (yyy-xxx) >>U 12) + ((yyy-xxx) & 0x00000800 ? 1 : 0) - addi t1,t1,%pcrel_lo(xxx) // ((yyy-xxx) & 0xfff) + addi t1,t1,%pcrel_lo(xxx) // ((yyy-xxx) & 0x00000fff) ... yyy: // the address: yyy is saved into t1 above ...