找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1926|回复: 10
打印 上一主题 下一主题
收起左侧

51单片机汇编语言怎样把数据表放在数据存储器中?

[复制链接]
跳转到指定楼层
楼主
ID:641358 发表于 2024-12-31 09:02 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
在c51中可以用 char code table[5]={xx,xx,xx,xx,xx}的数组把数据表存放在程序存储器中,是不可修改的。如果不用code,则table是存放在数据存储器中的,是可修改的。在汇编语言中用伪指令DB来存放数据表,但这个数据表是存放在程序存储器中的,不可修改。现在的问题是,在使用汇编语言编程时,怎样将一串数据存放在数据存储器中,可以修改它,也可以用MOV A,@R0的办法来查表,而不必用MOVC 指令来取数。








分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:73992 发表于 2025-1-1 20:16 | 只看该作者
程序运行的时候,依次将程序存储器中的数据表读写到指定内存。然后从内存引用
回复

使用道具 举报

板凳
ID:155507 发表于 2025-1-1 22:58 | 只看该作者
你可以用伪指令放进不同的段在储存器中的空间分配。




?DT?myvar  SEGMENT DATA         ; define a SEGMENT of class DATA
           RSEG    ?DT?myvar
VALUE:     DS      1            ; reserve 1 BYTE in DATA space


IO_PORT2   DATA    0A0H         ; special function register
VALUE2     DATA    20H          ; absolute memory location


?BI?mybits SEGMENT BIT          ; define a SEGMENT of class BIT
           RSEG    ?BI?mybits
FLAG:      DBIT    1            ; reserve 1 Bit in BIT space
P1         DATA    90H          ; 8051 SFR PORT1
GREEN_LED  BIT     P1.2         ; GREEN LED on I/O PORT P1.2


?ID?myvars SEGMENT IDATA        ; define a SEGMENT of class IDATA
           RSEG    ?EB?mybits
BUFFER:    DS      100          ; reserve 100 Bytes


?XD?my_seg SEGMENT XDATA                ; define a SEGMENT of class XDATA
           RSEG    ?ED?my_seg
XBUFFER:   DS      100                  ; reserve 100 Bytes


?CO?my_seg SEGMENT CODE                ; define a SEGMENT of class CODE
           RSEG    ?CO?my_seg
TABLE:     DB      1,2,4,8,0x10        ; a table with constant values




?PR?myprog SEGMENT CODE         ; define a segment for program code
           RSEG    ?PR?myprog
           MOV     A,IO_PORT2   ; direct memory access to DATA
           ADD     A,VALUE
           MOV     VALUE2,A
           MOV     R1,#VALUE    ; load address of VALUE to R1
           ADD     A,@R1        ; indirect memory access to VALUE


回复

使用道具 举报

地板
ID:624769 发表于 2025-1-2 00:34 | 只看该作者
不建议你学汇编,因为汇编需要对单片机工作原理有一定的理解,而你对单片机的工作原理好像一无所知。

单片机上电后,DATA (数据存储器)  中是随机值,并不会保留以前的任何数据, 因此,如果你需要把  数组(一串数据)放在 DATA (数据存储器)中, 首先,你需要把这些数据 利用 DB  放在 CODE(程序存储器) 中, 然后, 再在程序中, 通过  MOVC A,@A+DPTR  以及  MOV  @R1,A  把数组 存到 DATA (数据存储器)中。

真要从基础开始认真学汇编的话,是不会问这个问题的……
回复

使用道具 举报

5#
ID:235200 发表于 2025-1-2 09:09 | 只看该作者
要实现你的问题,只能采用数组形式存放,而且只能存放在内存中
回复

使用道具 举报

6#
ID:341045 发表于 2025-1-2 10:22 | 只看该作者
直接往里塞就行了呀。如MOV 30H,#1; MOV 31H,#2; MOV 32H,#3; MOV 33H,A
回复

使用道具 举报

7#
ID:641358 发表于 2025-1-3 15:17 | 只看该作者
lzts88 发表于 2025-1-2 10:22
直接往里塞就行了呀。如MOV 30H,#1; MOV 31H,#2; MOV 32H,#3; MOV 33H,A

这种方法我也想过,就是显得有点笨拙,但这应该是最基本的。
回复

使用道具 举报

8#
ID:641358 发表于 2025-1-3 16:49 | 只看该作者
csmyldl 发表于 2025-1-2 09:09
要实现你的问题,只能采用数组形式存放,而且只能存放在内存中

关键是汇编语言没有数组的说法。
回复

使用道具 举报

9#
ID:384109 发表于 2025-1-3 21:10 | 只看该作者
只能用MOV A,@R0的方式读写,存放的时候注意存放在一个连续区域就行了,数组也只不过是存在连续区域的数据而已
回复

使用道具 举报

10#
ID:641358 发表于 2025-1-5 19:21 | 只看该作者
人中狼 发表于 2025-1-3 21:10
只能用MOV A,@R0的方式读写,存放的时候注意存放在一个连续区域就行了,数组也只不过是存在连续区域的数据 ...

这还是一个数一个数的存,而不能像C语言一样一次性存储多个数据。
回复

使用道具 举报

11#
ID:155507 发表于 2025-1-5 22:51 | 只看该作者
tzqi 发表于 2025-1-5 19:21
这还是一个数一个数的存,而不能像C语言一样一次性存储多个数据。

因为在C语言,你在做数组到时候他就会带入,这一个程序。


  1. ;------------------------------------------------------------------------------
  2. ;  This file is part of the CX51 Compiler package
  3. ;  Copyright (c) 1988-2003 Keil Elektronik GmbH and Keil Software, Inc.
  4. ;
  5. ;  *** <<< Use Configuration Wizard in Context Menu >>> ***
  6. ;------------------------------------------------------------------------------
  7. ;  INIT_MX.A51:  This code is executed, if the application program contains
  8. ;                initialized variables at file level.
  9. ;
  10. ;  If you are using uVision2, just add the file as last file to your project.
  11. ;  *** IMPORTANT NOTE ***:  this file needs to be the last file of the linker
  12. ;  input list.  If you are using uVision2 this file should be therefore the
  13. ;  last file in your project tree.
  14. ;
  15. ;
  16. ;  To translate this file use Ax51 with the following invocation:
  17. ;
  18. ;     AX51 INIT_MX.A51
  19. ;
  20. ;  To link the modified INIT_MX.OBJ file to your application use the following
  21. ;  linker invocation:
  22. ;
  23. ;     LX51 your object file list, INIT.OBJ controls
  24. ;
  25. ;------------------------------------------------------------------------------
  26. ;
  27. ;  User-defined Watch-Dog Refresh.
  28. ;
  29. ;  If the C application contains many initialized variables uses a watchdog
  30. ;  it might be possible that the user has to include a watchdog refresh into
  31. ;  the initialization process. The watchdog refresh routine can be included
  32. ;  in the following MACRO and can alter all CPU registers except
  33. ;  DPTR.
  34. ;
  35. WATCHDOG        MACRO
  36.                                 ; Include any Watchdog refresh code here
  37.                 ENDM
  38. ;
  39. ;------------------------------------------------------------------------------
  40. ;
  41. ;<h>  Far Memory Support
  42. ;
  43. ;  If the C application contains variables in the far memory space that are
  44. ;  initialized, you need to set the following define to 1.
  45. ;
  46. ; --- Set XBANK = 1 when far variables should be initialized
  47. ; <q> XBANK: Initialize variables in far memory
  48. ;          <i>If the C application contains variables in the far memory space that are
  49. ;          <i>initialized, you need to check this box.
  50. $SET (XBANK = 1)
  51. ;
  52. ;</h>
  53. ;------------------------------------------------------------------------------

  54. ; Standard SFR Symbols
  55. ACC     DATA    0E0H
  56. DPL     DATA    82H
  57. DPH     DATA    83H

  58.                 NAME        ?C_INIT


  59. ?C_C51STARTUP        SEGMENT   CODE
  60. ?C_INITSEG        SEGMENT   CODE                ; Segment zur Initialisierung


  61. INIT_IorP       MACRO
  62. IorPData:                               ; If CY=1 PData Values
  63.                 CLR     A
  64.                 MOVC    A,@A+DPTR
  65.                 INC     DPTR
  66.                 MOV     R0,A            ; Start Address
  67. IorPLoop:       CLR     A
  68.                 MOVC    A,@A+DPTR
  69.                 INC     DPTR
  70.                 JC      PData
  71.                 MOV     @R0,A
  72.                 SJMP    Common
  73. PData:          MOVX    @R0,A
  74. Common:         INC     R0
  75.                 DJNZ    R7,IorPLoop
  76.                 JMP    Loop
  77.                 ENDM


  78.                 EXTRN CODE (MAIN)
  79.                 PUBLIC  ?C_START

  80.                 RSEG    ?C_C51STARTUP
  81. INITEND:        EJMP    MAIN

  82. $IF (XBANK = 0)
  83.                 INIT_IorP
  84. $ENDIF

  85. Bits:           CLR     A
  86.                 MOVC    A,@A+DPTR
  87.                 INC     DPTR
  88.                 MOV     R0,A
  89.                 ANL     A,#007H
  90.                 ADD     A,#Table-LoadTab
  91.                 XCH     A,R0
  92.                 CLR     C      
  93.                 RLC     A               ; Bit Condition to Carry
  94.                 SWAP    A
  95.                 ANL     A,#00FH
  96.                 ORL     A,#20H          ; Bit Address
  97.                 XCH     A,R0            ; convert to Byte Addressen
  98.                 MOVC    A,@A+PC
  99. LoadTab:        JC      Setzen
  100.                 CPL     A
  101.                 ANL     A,@R0
  102.                 SJMP    BitReady
  103. Setzen:         ORL     A,@R0
  104. BitReady:       MOV     @R0,A
  105.                 DJNZ    R7,Bits
  106.                 SJMP    Loop

  107. Table:          DB      00000001B
  108.                 DB      00000010B
  109.                 DB      00000100B
  110.                 DB      00001000B
  111.                 DB      00010000B
  112.                 DB      00100000B
  113.                 DB      01000000B
  114.                 DB      10000000B
  115.                

  116. ?C_START:      
  117.                 MOV     DPTR,#?C_INITSEG
  118. Loop:
  119.                 WATCHDOG
  120.                 CLR     A
  121.                 MOV     R6,#1
  122.                 MOVC    A,@A+DPTR
  123.                 JZ      INITEND
  124.                 INC     DPTR
  125.                 MOV     R7,A
  126.                 ANL     A,#3FH
  127.                 JNB     ACC.5,NOBIG
  128.                 ANL     A,#01FH
  129.                 MOV     R6,A
  130.                 CLR     A
  131.                 MOVC    A,@A+DPTR
  132.                 INC     DPTR
  133.                 JZ      NOBIG
  134.                 INC     R6
  135. NOBIG:          XCH     A,R7

  136. ; ---- Init for far Variables
  137. $IF (XBANK = 1)
  138.                 ANL     A,#0E0H
  139.                 CJNE    A,#0E0H,NOHDATA
  140. ;
  141. HPTRINIT:       CLR     A
  142.                 MOVC    A,@A+DPTR
  143.                 MOV     R3,A
  144.                 INC     DPTR
  145.                 CLR     A
  146.                 MOVC    A,@A+DPTR
  147.                 MOV     R2,A
  148.                 INC     DPTR
  149.                 CLR     A
  150.                 MOVC    A,@A+DPTR
  151.                 MOV     R1,A
  152.                 INC     DPTR
  153. HLOOP:          CLR     A
  154.                 MOVC    A,@A+DPTR
  155.                 EMOV        @PR0,A
  156.                 INC     DPTR
  157.                 INC     R1
  158.                 MOV     A,R1
  159.                 JNZ     HINC
  160.                 INC     R2
  161. HINC:           DJNZ    R7,HLOOP
  162.                 DJNZ    R6,HLOOP
  163.                 SJMP    Loop
  164. NOHDATA:
  165. $ENDIF
  166.                 ANL     A,#0C0H         ; Typ is in Bit 6 and Bit 7
  167.                 ADD     A,ACC
  168.                 JZ      IorPData
  169.                 JC      Bits

  170. XdataMem:       CLR     A
  171.                 MOVC    A,@A+DPTR
  172.                 INC     DPTR
  173.                 MOV     R2,A            ; High
  174.                 CLR     A
  175.                 MOVC    A,@A+DPTR
  176.                 INC     DPTR
  177.                 MOV     R0,A            ; LOW
  178. XLoop:          CLR     A
  179.                 MOVC    A,@A+DPTR
  180.                 INC     DPTR
  181.                 XCH     A,R0
  182.                 XCH     A,DPL
  183.                 XCH     A,R0
  184.                 XCH     A,R2
  185.                 XCH     A,DPH
  186.                 XCH     A,R2
  187.                 MOVX    @DPTR,A
  188.                 INC     DPTR
  189.                 XCH     A,R0
  190.                 XCH     A,DPL
  191.                 XCH     A,R0
  192.                 XCH     A,R2
  193.                 XCH     A,DPH
  194.                 XCH     A,R2
  195.                 DJNZ    R7,XLoop
  196.                 DJNZ    R6,XLoop
  197.                 SJMP    Loop

  198. $IF (XBANK = 1)
  199.                 INIT_IorP
  200. $ENDIF

  201.                 RSEG    ?C_INITSEG
  202.                 DB      0

  203. ;-------------------------------------------------------------------------
  204. ; STRUCTURE OF THE INITIALIZATION INFORMATION
  205. ; -------------------------------------------
  206. ; This section describes the initialization data generated by C51 for
  207. ; explicit variable initializations (in segment ?C_INITSEC).
  208. ;
  209. ; Explicit variable initilizations at C source level are stored by C51 in
  210. ; the segment ?C_INITSEC.  All partial segments are combined at linker level
  211. ; to one segment.  The segment end value DB 0 is taken from this library module
  212. ; INIT.A51.
  213. ;
  214. ; Structure of the ?C_INITSEC information:
  215. ;     Info (see below) [BYTE]                      ----+  repeated
  216. ;     additional info  [BYTES depend on Info]      ----+  repeated
  217. ;     0x00             [BYTE]   end of list mark
  218. ;
  219. ;  Info has the following format:
  220. ;
  221. ;  Bit      7  6  5  4  3  2  1  0
  222. ;  Info   T  T  B  L  L  L  L  L    T=Type  B=BIGBIT  L=LENGTH
  223. ;
  224. ;  If BIGBIT is set, another LENGTH BYTE FOLLOWS.  The LENGTH
  225. ;  info of the first byte is then the HIGH part.
  226. ;
  227. ;  Typ is one of the following:
  228. ;  0  := IDATA init values;  the following bytes follow:
  229. ;        -  1 byte address
  230. ;        -  init data bytes according LENGTH specification
  231. ;
  232. ;  1  := XDATA init values;  the following bytes follow:
  233. ;        -  2 byte address (high byte first)
  234. ;        -  init data bytes according LENGTH specification
  235. ;
  236. ;  2  := PDATA init values;  the following bytes follow:
  237. ;        -  1 byte address
  238. ;        -  init data bytes according LENGTH specification
  239. ;
  240. ;  3, BIGBIT=0  := BIT init values; the followign bytes follow:
  241. ;        -  1 byte for each bit according LENGTH specification
  242. ;           this byte has the following format:
  243. ;
  244. ;           Bit    7  6  5  4  3  2  1  0
  245. ;                  I  B  B  B  B  B  B  B   I := state of the bit
  246. ;                                           B := bit address
  247. ;
  248. ;  3, BIGBIT=1  := HDATA init values;   the following bytes follow:
  249. ;        -  another LENGTH byte (since BIGBIT is always 1)
  250. ;        -  3 byte address  (MSB first)
  251. ;        -  data bytes according LENGTH specification
  252. ;
  253. ;----------------------------------------------------------------------

  254.                 END

复制代码



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表