{"id":1254,"date":"2023-03-16T11:19:00","date_gmt":"2023-03-16T11:19:00","guid":{"rendered":"https:\/\/262235.xyz\/?p=1254"},"modified":"2023-03-16T11:19:00","modified_gmt":"2023-03-16T11:19:00","slug":"1254","status":"publish","type":"post","link":"https:\/\/lyvba.com\/index.php\/2023\/03\/16\/1254\/","title":{"rendered":"ChatGPT\u95ee\u7b54: RiscV \u6c47\u7f16\u600e\u4e48\u5199 1\u5230100 \u6570\u5b57\u4e4b\u548c\u4ee3\u7801   RISC-V \u6c47\u7f16\u8bed\u8a00\u5165\u95e8 add, sub, lw, sw, j \u5e38\u7528\u6307\u4ee4"},"content":{"rendered":"<h2>ChatGPT\u95ee\u7b54: RiscV \u6c47\u7f16\u600e\u4e48\u5199 1\u5230100 \u6570\u5b57\u4e4b\u548c\u4ee3\u7801<\/h2>\n<h3>ChatGPT: \u4ee5\u4e0b\u662f\u4f7f\u7528RISC-V\u6c47\u7f16\u8bed\u8a00\u8ba1\u7b971\u5230100\u4e4b\u548c\u7684\u4ee3\u7801\uff1a<\/h3>\n<pre><code># \u8bbe\u7f6e\u5bc4\u5b58\u5668 a0 \u4e3a 1\nli a0, 1\n# \u8bbe\u7f6e\u5bc4\u5b58\u5668 a1 \u4e3a 100\nli a1, 100\n# \u8bbe\u7f6e\u5bc4\u5b58\u5668 t0 \u4e3a 0\uff0c\u7528\u4e8e\u7d2f\u52a0\u548c\nli t0, 0\n\n# \u5faa\u73af\u5f00\u59cb\uff0c\u4f7f\u7528\u5bc4\u5b58\u5668 t1 \u5b58\u50a8\u5faa\u73af\u53d8\u91cf\naddi t1, zero, 0 # t1 = 0\nloop:\n    add t0, t0, a0   # \u7d2f\u52a0 a0 \u7684\u503c\u5230 t0\n    addi a0, a0, 1   # a0 \u81ea\u589e 1\n    addi t1, t1, 1   # t1 \u81ea\u589e 1\n    bne t1, a1, loop # \u5982\u679c t1 \u4e0d\u7b49\u4e8e a1 \u5219\u8df3\u8f6c\u56de loop\n\n# \u7a0b\u5e8f\u7ed3\u675f\uff0ct0 \u4e2d\u5b58\u50a8\u4e86 1 \u5230 100 \u7684\u548c<\/code><\/pre>\n<h3>\u89e3\u91ca\uff1a<\/h3>\n<p>\u9996\u5148\u4f7f\u7528 li \u6307\u4ee4\u5c06 1 \u548c 100 \u5206\u522b\u5b58\u50a8\u5230\u5bc4\u5b58\u5668 a0 \u548c a1 \u4e2d\uff0c\u7136\u540e\u4f7f\u7528 li \u6307\u4ee4\u5c06\u5bc4\u5b58\u5668 t0 \u7684\u521d\u59cb\u503c\u8bbe\u4e3a 0\uff0c\u7528\u4e8e\u7d2f\u52a0\u548c\u3002<br \/>\u63a5\u4e0b\u6765\u8fdb\u5165\u5faa\u73af\uff0c\u4f7f\u7528\u5bc4\u5b58\u5668 t1 \u5b58\u50a8\u5faa\u73af\u53d8\u91cf\uff0c\u521d\u59cb\u503c\u8bbe\u4e3a 0\u3002\u5728\u6bcf\u6b21\u5faa\u73af\u4e2d\uff0c\u4f7f\u7528 add \u6307\u4ee4\u5c06 a0 \u7684\u503c\u52a0\u5230 t0 \u4e2d\uff0c<br \/>\u7136\u540e\u4f7f\u7528 addi \u6307\u4ee4\u5c06 a0 \u52a0 1\uff0ct1 \u52a0 1\uff0c\u7ee7\u7eed\u4e0b\u4e00\u6b21\u5faa\u73af\u3002\u6700\u540e\u4f7f\u7528 bne \u6307\u4ee4\u5224\u65ad t1 \u662f\u5426\u7b49\u4e8e a1\uff0c\u5982\u679c\u4e0d\u76f8\u7b49\u5219\u8df3\u8f6c\u56de\u5faa\u73af\u5f00\u59cb\u5904\uff0c<br \/>\u7ee7\u7eed\u5faa\u73af\u3002\u5982\u679c\u76f8\u7b49\u5219\u8df3\u51fa\u5faa\u73af\uff0c\u7a0b\u5e8f\u7ed3\u675f\uff0ct0 \u4e2d\u5b58\u50a8\u4e86 1 \u5230 100 \u7684\u548c\u3002<\/p>\n<h2>\u5e38\u7528\u57fa\u7840\u6307\u4ee4<\/h2>\n<table>\n<thead>\n<tr>\n<th>Example Usage<\/th>\n<th>\u4e2d\u6587\u63cf\u8ff0<\/th>\n<th>Description<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>add t1,t2,t3<\/td>\n<td>\u52a0\u6cd5\uff1a\u8bbe t1 \u4e3a\uff08t2 \u52a0 t3\uff09<\/td>\n<td>Addition: set t1 to (t2 plus t3)<\/td>\n<\/tr>\n<tr>\n<td>addi t1,t2,-100<\/td>\n<td>\u52a0\u6cd5\u7acb\u5373\u6570\uff1a\u5c06 t1 \u8bbe\u7f6e\u4e3a\uff08t2 \u52a0\u4e0a\u5e26\u7b26\u53f7\u7684 12 \u4f4d\u7acb\u5373\u6570\uff09<\/td>\n<td>Addition immediate: set t1 to (t2 plus signed 12-bit immediate)<\/td>\n<\/tr>\n<tr>\n<td>sub t1,t2,t3<\/td>\n<td>\u51cf\u6cd5\uff1a\u5c06 t1 \u8bbe\u7f6e\u4e3a\uff08t2 \u51cf\u53bb t3\uff09<\/td>\n<td>Subtraction: set t1 to (t2 minus t3)<\/td>\n<\/tr>\n<tr>\n<td>mul t1,t2,t3<\/td>\n<td>\u4e58\u6cd5\uff1a\u5c06t1\u8bbe\u4e3at2*t3\u7684\u4f4e32\u4f4d<\/td>\n<td>Multiplication: set t1 to the lower 32 bits of t2*t3<\/td>\n<\/tr>\n<tr>\n<td>div t1,t2,t3<\/td>\n<td>\u9664\u6cd5\uff1a\u5c06 t1 \u8bbe\u7f6e\u4e3a t2\/t3 \u7684\u7ed3\u679c<\/td>\n<td>Division: set t1 to the result of t2\/t3<\/td>\n<\/tr>\n<tr>\n<td>lw t1, -100(t2)<\/td>\n<td>\u5c06 t1 \u8bbe\u7f6e\u4e3a\u6709\u6548\u5185\u5b58\u5b57\u5730\u5740\u7684\u5185\u5bb9<\/td>\n<td>Set t1 to contents of effective memory word address<\/td>\n<\/tr>\n<tr>\n<td>sw t1, -100(t2)<\/td>\n<td>Store word\uff1a\u5c06t1\u7684\u5185\u5bb9\u5b58\u5165\u6709\u6548\u5185\u5b58\u5b57\u5730\u5740<\/td>\n<td>Store word : Store contents of t1 into effective memory word address<\/td>\n<\/tr>\n<tr>\n<td>beq t1,t2,label<\/td>\n<td>Branch if equal : \u5982\u679c t1 \u548c t2 \u76f8\u7b49\u5219\u5206\u652f\u5230\u6807\u7b7e\u5730\u5740\u5904\u7684\u8bed\u53e5<\/td>\n<td>Branch if equal : Branch to statement at label's address if t1 and t2 are equal<\/td>\n<\/tr>\n<tr>\n<td>bne t1,t2,label<\/td>\n<td>Branch if not equal : \u5982\u679c t1 \u548c t2 \u4e0d\u76f8\u7b49\u5219\u5206\u652f\u5230\u6807\u7b7e\u5730\u5740\u5904\u7684\u8bed\u53e5<\/td>\n<td>Branch if not equal : Branch to statement at label's address if t1 and t2 are not equal<\/td>\n<\/tr>\n<tr>\n<td>blt t1,t2,label<\/td>\n<td>Branch if less than\uff1a\u5982\u679c t1 \u5c0f\u4e8e t2\uff0c\u5219\u5206\u652f\u5230\u6807\u7b7e\u5730\u5740\u5904\u7684\u8bed\u53e5<\/td>\n<td>Branch if less than: Branch to statement at label's address if t1 is less than t2<\/td>\n<\/tr>\n<tr>\n<td>bge t1,t2,label<\/td>\n<td>Branch if greater than or equal\uff1a\u5982\u679c t1 \u5927\u4e8e\u6216\u7b49\u4e8e t2\uff0c\u5219\u5206\u652f\u5230\u6807\u7b7e\u5730\u5740\u5904\u7684\u8bed\u53e5<\/td>\n<td>Branch if greater than or equal: Branch to statement at label's address if t1 is greater than or equal to t2<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>\u5e38\u7528\u4f2a\u6307\u4ee4<\/h2>\n<table>\n<thead>\n<tr>\n<th>Example Usage<\/th>\n<th>\u4e2d\u6587\u63cf\u8ff0<\/th>\n<th>Description<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>la t1,label<\/td>\n<td>\u52a0\u8f7d\u5730\u5740\uff1a\u5c06 t1 \u8bbe\u7f6e\u4e3a\u6807\u7b7e\u7684\u5730\u5740<\/td>\n<td>Load Address : Set t1 to label's address<\/td>\n<\/tr>\n<tr>\n<td>li t1,-100<\/td>\n<td>\u7acb\u5373\u52a0\u8f7d\uff1a\u5c06 t1 \u8bbe\u7f6e\u4e3a 12 \u4f4d\u7acb\u5373\u6570\uff08\u7b26\u53f7\u6269\u5c55\uff09<\/td>\n<td>Load Immediate : Set t1 to 12-bit immediate (sign-extended)<\/td>\n<\/tr>\n<tr>\n<td>li t1,10000000<\/td>\n<td>\u7acb\u5373\u52a0\u8f7d\uff1a\u5c06 t1 \u8bbe\u7f6e\u4e3a 32 \u4f4d\u7acb\u5373\u6570<\/td>\n<td>Load Immediate : Set t1 to 32-bit immediate<\/td>\n<\/tr>\n<tr>\n<td>mv t1,t2<\/td>\n<td>MoVe\uff1a\u5c06 t1 \u8bbe\u7f6e\u4e3a t2 \u7684\u5185\u5bb9<\/td>\n<td>MoVe : Set t1 to contents of t2<\/td>\n<\/tr>\n<tr>\n<td>neg t1,t2<\/td>\n<td>NEGate \uff1a\u5c06 t1 \u8bbe\u7f6e\u4e3a t2 \u7684\u5426\u5b9a<\/td>\n<td>NEGate : Set t1 to negation of t2<\/td>\n<\/tr>\n<tr>\n<td>j label<\/td>\n<td>\u8df3\u8f6c\uff1a\u8df3\u8f6c\u5230\u6807\u7b7e\u5904\u7684\u8bed\u53e5<\/td>\n<td>Jump : Jump to statement at label<\/td>\n<\/tr>\n<tr>\n<td>jr t0<\/td>\n<td>\u8df3\u8f6c\u5bc4\u5b58\u5668\uff1a\u8df3\u8f6c\u5230 t0 \u4e2d\u7684\u5730\u5740<\/td>\n<td>Jump Register: Jump to address in t0<\/td>\n<\/tr>\n<tr>\n<td>nop<\/td>\n<td>\u65e0\u64cd\u4f5c<\/td>\n<td>NO OPeration<\/td>\n<\/tr>\n<tr>\n<td>ret<\/td>\n<td>\u8fd4\u56de\uff1a\u4ece\u5b50\u7a0b\u5e8f\u8fd4\u56de<\/td>\n<td>Return: return from a subroutine<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>RISC-V\u6c47\u7f16\u8ba1\u7b970\u81f3100\u6574\u6570\u4e4b\u548c\uff0c\u4f7f\u7528 <code>gcc -S<\/code> \u6c47\u7f16\u540e\u4fee\u6539\u5f97\u5230\u4ee3\u7801<\/h2>\n<pre><code># int a = 0;    \/\/ \u8ba1\u7b97 0\u81f3100\u6574\u6570\u4e4b\u548c\n# for(int i =0; i &lt;= 100 ; i++){\n#   a += i;   }\n.data\nA: .word 0x10010000\n .text\nmain:\n    la t1, A   # \u52a0\u8f7d \u53d8\u91cf a \u7684\u5730\u5740\n    li t2, 0x10010004  #  i\n        sw      zero, (t1)     # a = 0\n        sw      zero, (t2)   # i = 0\n        j      FOR\n.L3:\n        lw      a5, (t1)\n        mv      a4,a5\n        lw      a5, (t2)\n        addw    a5,a4,a5   # a += i;\n        sw      a5, (t1) # \u4fdd\u5b58 a\n\n        lw      a5, (t2)   # i++\n        addiw   a5,a5,1\n        sw      a5, (t2) #   \u4fdd\u5b58 i\nFOR:\n        lw      a5, (t2)  # \u8bfb\u53d6 i\n         sext.w  a4, a5\n        li      a5,100       # i &lt;= 100\n        ble     a4,a5, .L3\n\n        lw     a0,  (t1)   #  \u6253\u5370 a \u503c 5050\n        li      a7,1\n        ecall<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>ChatGPT\u95ee\u7b54: RiscV \u6c47\u7f16\u600e\u4e48\u5199 1\u5230100 \u6570\u5b57\u4e4b\u548c\u4ee3\u7801 ChatGPT: \u4ee5\u4e0b\u662f\u4f7f\u7528 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[72],"class_list":["post-1254","post","type-post","status-publish","format-standard","hentry","category-learn","tag-riscv"],"_links":{"self":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts\/1254","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/comments?post=1254"}],"version-history":[{"count":0,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts\/1254\/revisions"}],"wp:attachment":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/media?parent=1254"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/categories?post=1254"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/tags?post=1254"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}