0

I am new to assembly programming and trying to interpret the following code where I've to print an array present in the .data section : Here is the code:

%macro print 2
    mov rax,1
    mov rdi,1
    mov rsi,%1
    mov rdx,%2
    syscall
%endmacro

%macro exit 0
    mov rax,60
    mov rdi,0
    syscall
%endmacro


section .data

    msg db 10,"Array is : ",10
    len equ $-msg   

    array dq 11234H, 0AB32H, -3326H, 056BH

    newline db 10

section .bss

    buff resb 16;

section .code
global _start
_start:
    print msg,len
    mov rsi,array
    mov rcx,4
    back:
        mov rbx,[rsi]
        push rsi
        push rcx
        call HextoASCII
        print newline,1
        pop rcx
        pop rsi
        add rsi,8
    loop back

    exit


HextoASCII:
    mov rsi,buff
    mov rcx,16
    back1:
        rol rbx,4
        mov al,bl
        and al,0fh
        cmp al,9h
        jbe add_30h
        add al,7h
        add_30h:
            add al,30h

        mov [rsi],al
        inc rsi
    loop back1
    print buff,16
ret

I have a few questions to ask to clear my doubts :

  1. What is the default value of the variable present in the .bss section ?

  2. The size of msg is 10 bytes(max size of msg), but when I put more characters into it, then it still prints the entire msg even though the string size exceeds its maximum limit(10 bytes).

  3. If the numbers given in the array have their last(highest significant digit) as non-zero, doesn't it mean that the number is negative ie.- Isn't 11234H present in the array a negative number because I assume that the highest significant bits are present as 1 (FFF11234H) in the memory. I think that for a number to be non-negative its highest digit must be zero(011234), so that the higher ordered bits are stored as 0 in memory and make the number positive. If I'm wrong here then another thing to ask is whether FFFFFFFFH is -1 or a large positive number.

  4. I am getting confused by the instruction

inc rsi

It is said that rsi is incremented by 1. But what is 1 here,bit or byte or 8 byte(size of rsi).

  1. Adding 30H or 37H converts the hexadecimal digit to 39H or 41H and these are hexadecimal representation of 9 and A in ASCII but I do not understand why printing 39H or 41H will yield the result 9 or A on my monitor and not 39H or 41H itself. Is it that whatever our result is, the assembler prints its ASCII equivalent on the monitor. Also, In what form is the input that we give through keyboard interpreted by assembler/machine and if it is ASCII then do I need to convert it back to HEX explicitly for later calculations ?

  2. After adding 30H or 37H,the single digit number(0H-9H or AH-FH) gets converted into double digit ranging from 30H-39H and 41H - 46H and so when we move this to buff, won't it actually occupy double the size of array element into buff to store it after the conversion ? I assume that earlier the digit took 4 bits and now it takes 8 bit after the conversion(9h is 4 bit and 39h is 8 bit). Do correct me, if I'm wrong here. If I'm right then, Is that the reason why buff is taken as 16 bytes when each array element size is just 8 bytes(quadword).

  3. I understand the logic of HextoASCII which takes the highest digit of the number and stores it in buff and this goes on and later buff is printed but won't the number be printed in reverse manner because the highest digit of array element be stored at the least significant location of buff and as rsi is incremented the next digit will be added at the higher position of buff and so if we print buff then the most significant number gets placed at the least significant location of buff.ie-12H is store as 21 in buff because 1 is stored first in buff and later 2 is stored. Is there any importance of little endian and big endian here to store the digits ? Do explain, if yes.

  4. In the print macro, 2nd argument is always the size of the variable that is to be printed.Here, the size of buff is 16 bytes and hence the second argument is 16. But if I make the 2nd argument as 8 then why is half of the digits of every array element not being printed but still the entire 8 digits are getting printed. Also, present me a case where half of the digits of every element of array will be printed, In that case will the higher significant 4 digits be printed or the lower significant 4 digits be printed ? Also, why ?

4
  • 1
    This is too many separate questions for one post on Stack Overflow. Definitely try to limit things to one question per question-post, with at most a small bonus question that's not really related to the main question. How to Ask. Your questions are at least reasonably clear and easy to understand what you're asking, though. Commented Mar 29, 2018 at 2:13
  • Well,adding a seperate question on a different post will be a tough job because all of them were related to same program. Commented Mar 29, 2018 at 5:05
  • The question about the BSS doesn't depend at all on the code in the .text section. You could have just asked it as a separate question, maybe with a 2-line example. The inc rsi question is again totally independent of all surrounding code; it's just a question about one instruction on its own. Having multiple questions about different parts of the same complete program doesn't make them related. You could ask at least a dozen different things about a Hello World in C, but that doesn't mean they belong in the same question. Commented Mar 29, 2018 at 17:50
  • Will not happen again !! Apologies being a newbie. Commented Mar 29, 2018 at 19:10

1 Answer 1

3
  1. Zero.
  2. 10 is not the size of msg. Those are just embedded line feeds. The size is calculated as len equ $-msg and thus will always match the length of the text you provide.
  3. The sign bit is the most significant bit according to the size. Since you have qwords, 11234H is 0000000000011234H and is positive. FFFFFFFFH is a large positive number. FFFFFFFFFFFFFFFFH could be an even larger number or -1, depending on whether you interpret it as unsigned or signed.
  4. Nothing, it's just 1. It's just adding one to the value in rsi. That will mean 1 byte when used as an address later.
  5. That's just because your terminal uses ascii codes and hence will print 0 for a value of 30h (and so on). Yes, you will need to convert from ascii to binary if you read text.
  6. That is correct.
  7. You described it correctly, but that is not reversed. Humans start with the most significant digit first. So it makes sense that the code puts that first and increments rsi to put the other digits after it. Not sure why you think that is reversed.
  8. Because print is invoked for each array element separately, hence shortening the output applies to each element not to the whole output. It will be the higher digits of course, since that's how they are in memory. See point 7, above.
Sign up to request clarification or add additional context in comments.

4 Comments

In 5. you mean that always the ASCII equivalent of hex is outputted on terminal ? 7. Reverse because as rsi increments, it points to higher address in buff and rotation of rbx the 2nd time assigns this 2nd highest digit to the location at the new incremented rsi location. So, digits of array element present at higher memory location are present at the lower memory location in buff and as we print buff, the number is printed reversed. If that is wrong, then how is buff printed ? ie- from higher memory location to lower or lower to higher ?
Consult an ascii table. You send a code, the terminal shows the matching character. / It's printed from low address to high address as usual. And low address has the high order digit, as expected. Nothing is reversed.
Thanks! I didn't get 5. completely - Will buff be double the size of the array element.I think yes. But if so, then let's suppose that the no. is 0FH then it will be stored in buff as F00000000H.Seven 30h will be stored at least memory location and 46h at highest position. Now, during printing 30h will be printed first and so on.. but how a particular 30h will be stored in buff. ie - as 03 or 30 and how will it be combined represented to make the terminal know that 30h are a combination and not isolated 3h and 0h ?
ascii uses bytes. Hence there can be no confusion about 3, 0 or 03h. You put 30h into that byte, that's what the terminal will use.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.