{"id":778,"date":"2022-08-30T15:07:01","date_gmt":"2022-08-30T15:07:01","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/how-to-relace-byte-of-32bit-variable-in-inline-assembly-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:07:01","modified_gmt":"2022-08-30T15:07:01","slug":"how-to-relace-byte-of-32bit-variable-in-inline-assembly-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/how-to-relace-byte-of-32bit-variable-in-inline-assembly-collection-of-common-programming-errors\/","title":{"rendered":"How to relace byte of 32bit variable in inline assembly?-Collection of common programming errors"},"content":{"rendered":"<p>I want to replace the highest byte of 32bit value with inline assembly, following code writes buffer to FRAM memory with spi interace:<\/p>\n<pre><code>#define _load_op_code(op_code, addr)\\\n  __asm__ __volatile__ (\\\n  \" ldi %D0, %1\"     \"\\n\\t\"\\\n  : \"=d\"  ((uint32_t)addr)\\\n  : \"M\" (op_code)\\\n  )\n\n#define SMEM_WREN   0x06\n#define SMEM_WRITE  0x02\n\nvoid fram_write(uint32_t addr, uint8_t *buf, uint16_t len) {\n  FRAM_SELECT();\n  spi_send_char(SMEM_WREN);\n  FRAM_DESELECT();\n  _load_op_code(SMEM_WRITE, addr);\n  FRAM_SELECT();\n  spi_send_32b(addr);\n  spi_send(buf, len);\n  FRAM_DESELECT();\n}\n<\/code><\/pre>\n<p>after _load_op_code() inline assembly addr variable gets cluttered &#8211; compiler use registers allocated for addr as temporary registers for other operations and i lose original addr value. addr is in fact 24bit variable. Any idea whats wrong with this code?<\/p>\n<ol>\n<li>\n<p>The original value of addr is lost because it is overwritten by the asm statement with SMEM_WRITE and 3 undefined bytes. From the GCC manual:<\/p>\n<blockquote>\n<p>The ordinary output operands must be write-only; GCC assumes that the values in these operands before the instruction are dead and need not be generated. Extended asm supports input-output or read-write operands. Use the constraint character \u2018+\u2019 to indicate such an operand and list it with the output operands.<\/p>\n<\/blockquote>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 22:42:43. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I want to replace the highest byte of 32bit value with inline assembly, following code writes buffer to FRAM memory with spi interace: #define _load_op_code(op_code, addr)\\ __asm__ __volatile__ (\\ &#8221; ldi %D0, %1&#8243; &#8220;\\n\\t&#8221;\\ : &#8220;=d&#8221; ((uint32_t)addr)\\ : &#8220;M&#8221; (op_code)\\ ) #define SMEM_WREN 0x06 #define SMEM_WRITE 0x02 void fram_write(uint32_t addr, uint8_t *buf, uint16_t len) { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-778","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/778","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=778"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/778\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=778"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=778"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=778"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}