Search code examples
windowsassemblytasm

How data is stored in memory or register


I m new to assembly language and learning it for exams. I ama programmer and worked in C,C++, java, asp.net.

I have tasm with win xp.

I want to know How data is stored in memory or register. I want to know the process. I believe it is something like this:

While Entering data, eg. number:

Input Decimal No. -> Converted to Hex -> Store ASCII of hex in registers or memory.

While Fetching data:

ASCII of hex in registers or memory -> Converted to Hex -> Show Decimal No. on monitor.

Is it correct. ? If not, can anybody tell me with simple e.g

Ok, Michael: See the code below where I am trying to add two 1 digit numbers to display 2 digit result, like 6+5=11

Sseg segment stack

ends

code segment
;30h to 39h represent numbers 0-9

MOV     BX, '6' ; ASCII CODE OF 6 IS STORED IN BX, equal to 36h
ADD     BX, '5' ; ASCII CODE OF 5 (equal to 35h) IS ADDED IN BX, i.e total is 71h 

Thanks Michael... I accept my mistake....

Ok, so here, BX=0071h, right ? Does it mean, BL=00 and BH=71 ?

However, If i do so, I can't find out how to show the result 11 ?

Hey Blechdose,

Can you help me in one more problem. I am trying to compare 2 values. If both are same then dl=1 otherwise dl=0. But in the following code it displays 0 for same values, it is showing me 0. Why is it not jumping ?

sseg segment stack
ends

code segment
    assume cs:code
    mov dl,0

    mov ax,5
    mov bx,5
    cmp ax,bx
    jne NotEqual
    je equal

NotEqual:
    mov dl,0
    add dl,30h
    mov ah,02h
    int 21h

    mov ax,4c00h
    int 21h

equal:  mov dl,1
    add dl,30h
    mov ah,02h
    int 21h

    mov ax,4c00h
    int 21h

code ends
end NotEqual
end equal

Solution

  • Registers consist of bits. A bit can have the logic value 0 or 1. It is a "logic value" for us, but actually it is represented by some kind of voltage inside the hardware. For example 4-5 Volt is interpreted as "logic 1" and 0-1 Volt as "logic 0". The BX register has 16 of those bits.

    Lets say the current content of BX(Base address register) is: 0000000000110110. Because it is very hard to read those long lines of 0s and 1s for humans, we combine every 4 bits to 1 Hexnumber, to get a more readable format to work with. The CPU does not know what a Hex or decimal number is. It can only work with binary code. Okay, let us use a more readable format for our BX register:

    0000 0000 0011 0110  (actual BX content)
    0    0    3    6     (HEX format for us)
                   54    (or corresponding decimal value)
    

    When you send this value (36h), to your output terminal, it will interpret this value as an ASCII-charakter. Thus it will display a "6" for the 36h value.

    When you want to add 6 + 2 with assembly, you put 0110 (6) and 0010 (2) in the registers. Your assembler TASM is doing the work for you. It allows you to write '6' (ASCII) or 0x6 (hex) or even 6 (decimal) in the asm-sourcecode and will convert that for you into a binary number, which the register accepts. WARNING: '6' will not put the value 6 into the register, but the ASCII-Code for 6. You cannot calculate with that directly.

    Example: 6+2=8

    mov BX, 6h    ; We put 0110 (6) into BX. (actually 0000 0000 0000 0110, 
                  ; because BX is 16 Bit, but I will drop those leading 0s)
    add BX, 2h    ; we add 0010 (2) to 0110 (6). The result 1000 (8) is stored in BX.
    add BX, 30h   ; we add 00110000 (30h). The result 00111000 (38h) is stored in BX.
                  ; 38h is the ASCII-code, which your terminal output will interpret as '8'
    

    When you do a calculation like 6+5 = 11, it will be even more complicated, because you have to convert the result 1011 (11) into 2 ASCII-Digits '1' and '1' (3131h = 00110001 00110001)

    After adding 6 (0110) + 5 (0101) = 11 (1011), BX will contain this (without blanks):

    0000 0000 0000 1011 (binary)
       0    0    0    B (Hex)
                     11 (decimal)
    |__________________|
            BX
    |________||________|
        BH        BL
    

    BH is the higher Byte of BX, while BL is the lower byte of BX. In our example BH is 00h, while BL contains 0bh.

    To display your summation result on your terminal output, you need to convert it to ASCII-Code. In this case, you want to display an '11'. Thus you need two times a '1'-ASCII-Character. By looking up one of the hunderds ASCII-tables on the internet, you will find out, that the Code for the '1'-ASCII-Charakter is 31h. Consequently you need to send 3131h to your terminal:

    0011 0001 0011 0001 (binary)
       3    1    3    1 (hex)
                  12593 (decimal)
    

    The trick to do this, is by dividing your 11 (1011) by 10 with the div instruction. After the division by 10 you get a result and a remainder. you need to convert the remainder into an ASCII-number, which you need to save into a buffer. Then you repeat the process by dividing the result from the last step by 10 again. You need to do this, until the result is 0. (using the div operation is a bit tricky. You have to look that up by yourself)

    binary (decimal):
    divide 1011 (11) by 1010 (10):
    result: 0001 (1) remainder: 0001 (1) -> convert remainderto ASCII
    divide result by 1010 (10) again:
    result: 0000 (1) remainder: 0001 (1) -> convert remainderto ASCII