1

The below code is a code I found from a site to study about assambly more.But I don't know exactly what do this section of code. For example what do .ORG 140 do? Why 140? Or in this line : .DB 0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F how we write them? from where?

.ESEG ;7segment LUT
.ORG 140
.DB 0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
mena
  • 15
  • 5
  • 4
    It’s just defining some data (a lookup table) in EEPROM: https://onlinedocs.microchip.com/pr/GUID-E06F3258-483F-4A7B-B1F8-69933E029363-en-US-2/index.html?GUID-66A828FE-6727-4C39-99E3-F62595939B96 – Paul R Jan 20 '23 at 08:17
  • 3
    the comment already says `7segment LUT` so it's a lookup table for 7-segment LED, for example 0x06 enables 2 segments of the 1, and 0x07 is for 7 – phuclv Jan 20 '23 at 08:24
  • what do you mean 2 segments of the 1? you mean 0x3F show 0? – mena Jan 20 '23 at 08:27
  • 2 segments **for** the digit `1`, to display it on a https://en.wikipedia.org/wiki/Seven-segment_display - it has 2 vertical segments lit, all the rest dark. I think @phuclv meant it as "2 segments *of* the pattern of segments that represents the digit 1". – Peter Cordes Jan 20 '23 at 10:58

1 Answers1

3

.ESEG ;7segment LUT

.ESEG specifies the EEPROM memory section, with a comment on what's the purpose of the data

.ORG 140

This specifies that the following data will start at address 140. The semantics of .org depends on the assembler brand you are using.

.DB 0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F

This are 10 bytes of data in a look-up table (LUT) that describe which segments of a 7-segment display have to be lit to show a specific digit. The digit is used as an index into the array, where indices start at an offset of 0. Hence, the 1st entry encodes which segments have to be lit to show a 0.

Written in binary, at the specified offsets we have the following values:

0 → 011 1111
1 → 000 0110
2 → 101 1011
3 → 100 1111
4 → 110 0110
5 → 110 1101
6 → 111 1101
7 → 000 0111
8 → 111 1111
9 → 110 1111

This means the 7-segment display(s) are "connected" / "wired" to the respective bits as follows:

 0000
5    1
5    1
 6666    
4    2
4    2
 3333

For example, a 4 has bits 1, 2, 5 and 6 on, which is the following pattern that resembles a 4 when lit:

 
5    1
5    1
 6666    
     2
     2

Some 7-segment displays feature a dot at the lower left or lower right, so that decimal fractions like 3.14 can be displayed. In this case one would wire the . to bit 7.

And in order to represent hexadecimal digits A, b, c, d, E and F, one could extend the array in the obvious way like

10 → 111 0111 ; A
11 → 111 1100 ; b
...

For negative numbers, encode - as

− → 100 0000 ; minus
emacs drives me nuts
  • 2,785
  • 13
  • 23
  • I was just about to answer this one, but I think you did a better job of explaining it than I would have. The way you used numbers to write the digits made it much clearer. Personally, had I written the original source code I would have specified those constants as binary to begin with and commented what numbers they were. – puppydrum64 Jan 20 '23 at 10:39
  • Your explanation was excellent. Thanks . And can You please tell me Why is this part of the code written like this? What is the function of the number 129 here? What does it have to do with the number 0? `.eseg .org 0b10000001;129 .db 0 .org 0b10000010;130 .db 1` @emacsdrivesmenuts – mena Jan 20 '23 at 15:54
  • @mena: Is assembly, and when you write it by hand you will have to allocate all objects by hand: Finding a place in the respective memory section that's still free, then place the object there. You have to specify the (start) address as a magic number so the assembler knows where to put it. And you have to use that number (address) to access it. Why they are using EEPROM I don't know, you'll have to ask the author. Usually one would use Flash to store such data, except you want to change non-volatile data at runtime like config data. But I don't see that use case for 7-segment data. – emacs drives me nuts Jan 20 '23 at 19:41
  • @emacsdrivesmenuts Chances are that's what's required for a valid "cartridge header" (usually some piece of data that needs to be at the beginning of your program in order for it to properly run. It's pretty common on low-level hardware.) – puppydrum64 Jan 24 '23 at 11:13
  • @puppydrum64: AVR doesn't need such a thing. The code just starts running at the reset vector, and from there you usually jump to the start-up code and then etc.etc. – emacs drives me nuts Jan 25 '23 at 12:01
  • Ah, so it uses a vector table. – puppydrum64 Jan 25 '23 at 12:35