0

I am a beginner in assembly programming and want to clarify some of my doubts that aroused in interpreting this 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
%macro accept 2
    mov rax,0
    mov rdi,0
    mov rsi,%1
    mov rdx,%2
    syscall
%endmacro
section .data

    a dw 123AH



section .bss
    b resb 2;       
    buff resb 2;

section .code
global _start
_start:
    accept b,2
    mov rsi,a           ;3
    mov rbx,[rsi]           ;6
    call hextoascii
    mov rsi,buff
    exit
hextoascii:
    mov rcx,4
    mov rsi,buff
    back:
        rol bx,4
        mov al,bl
        and al,0Fh
        cmp al,09H
        jbe add30h
        add al,7h
        add30h:
            add al,30h
        mov [rsi],al
        inc rsi         ;4
    loop back
    print buff,2            ;7


ret

I have a few questions to ask :

  1. Is there any difference between the following two with respect to the their storage in memory or any other a) a resb 2 b) a resw 1
  2. If I accept a variable from user then I have to reserve another byte for the "Enter" key pressed. Is it so ? If yes, then will the ascii value of "Enter" be concatenated as the last byte of the variable ?
  3. The instruction : mov rsi,a (a=1234H) To which memory location of 'a' does rsi points to ? ie- does it store the address of 4 or the address of 1.

  4. If I increment rsi then will it point to next digit of the variable a ? How shall I interpret "inc rsi" in the 4th last line of the code ? By how much postion does rsi gets incremented assuming that it currently points to the base address of "a" ? And will the increment depend on the size of varaible that it points to ?

  5. How is 'a' present in memory ? ie- Is 1 present at the lowest address or present at the highest address with respect to other digits?

  6. In the instruction : mov rbx,[rsi] How much of 'a' goes into rbx and what is the maximum limit of 'a' that rbx can store inside it ?

  7. In the instruction : print buff,4 It is printing only half the value of 'a' (12) and not '1234' completely. I have read that the 2nd argument of print macro should be the size of the variable (here 'buff') but since the size of buff is 2 bytes, it is just printing the half and not completely. Rather if I replace 2 by 4, then complete '1234' gets printed. What does the size mean here ? Is it the no. of bytes or something else ?
4
  • Part 4 is a duplicate of your previous question that you posted from another account with the same name. stackoverflow.com/questions/49541502/…. inc rsi increments the 64-bit integer value by 1. When used as a pointer, that means the next byte, always, regardless of anything else. Commented Mar 29, 2018 at 17:53
  • Ah, duplicate account ... Commented Mar 29, 2018 at 17:54
  • @Jacob: Use a debugger (like GDB) to single-step through your code, and do stuff like print /x *(char*)$rsi (or x /xbc $rsi) to eXamine a byte in memory to see what RSI is pointing to. You can answer most of these questions yourself with a little experimentation. Commented Mar 29, 2018 at 17:56
  • Well, I didn't knew the way the variables are storing the values(little endian) and that confused me so much that I had to ask the questions with respect to a new code !! I couldn't post a new question with that account !! Commented Mar 29, 2018 at 17:58

1 Answer 1

3
  1. No.
  2. Yes.
  3. x86 is little endian, so the first byte is the least significant byte, that is 34h.
  4. No, a hex digit is 4 bits (half a byte). You can only point to bytes. inc rsi increments by 1, which, when used as address is 1 byte.
  5. See point #3, above.
  6. Since you are loading into rbx which is a 64 bit register, that's how much you will get.
  7. The length of the text to print. The code is broken because it's writing 4 bytes into a buffer of 2 bytes.
Sign up to request clarification or add additional context in comments.

9 Comments

In 3. what is 34h since 'a' is 123AH ? When I print "buff " then the first 2 bytes get printed and not the last 2. ie- 12 which is the MSB gets printed and not 34.
The hextoascii prints in big endian (human readable) format. The cpu stores the binary data in little endian.
1)You didn't answer about the 34h mentioned in above comment. 2) You mean buff stores the result as A321 and gets it printed as 123AH 3)I still didn't get what will rsi point to ? If I use a loop to first point rsi to the variable "a" and then increment it to simultaneously print the variable,what will be the sequence of this print ?
In your question you claimed a=1234H. I see in the code it's actually 123AH. I used 1234. No, buff already stores the text to be printed. Only a stores the bytes 3A, 12 or 34, 12.
Is the sequence of storage of 123A opposite in rbx ? ie- does rbx stores it in the simple way bcz every rotation gives me the highest digit at then the end place ? Can you give me the value of buff and rbx in every loop and also where does rsi points to buff in every loop ? Why does 'a' follows the little endian and not buff ? Can you give me any resource to figure out the way this is working ?
|

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.