0

I am doing assembly programming and trying to accept a number from the user in a variabel num1 but some garbage is getting printed on the terminal. The image of the output is given in the desciption. Explain Why ?

%macro print 2
    mov rax,1
    mov rdi,1
    mov rsi,%1
    mov rdx,%2
    syscall     
%endmacro
%macro accept 2
    mov rax,0
    mov rdi,0
    mov rsi,%1
    mov rdx,%2
    syscall
%endmacro
%macro exit 0
    mov rax,60
    mov rdi,0
    syscall
%endmacro

section .bss
    num1 resb 5         ;1 extra for enter key  
    choice resb 2
    tempbuff resb 8
section .data

    enter1 dq 10,"num1 : ",
    len1 equ $-enter1


    newline db 0AH

section .code
global _start
_start:
    print enter1,len1
    accept num1,5
    exit

enter image description here

asn
  • 2,408
  • 5
  • 23
  • 37
  • 2
    You used `dq` for the `enter1` message instead of `db`. – Jester Mar 30 '18 at 11:46
  • So what will happen if I use dq and not db ? Does db mean that every character occuies 1 byte and if dq then every char. occupies 8 bytes ? – asn Mar 30 '18 at 11:52
  • 2
    Your initial `10` will take 8 bytes. The characters of the string will be packed by the assembler, but even so the total length will be a multiple of 8, hence you might get extra garbage included at the end. Learn to use a debugger or at least get a listing from `nasm` to see what it is doing. – Jester Mar 30 '18 at 11:57
  • I understood that every character will take 8 bytes but didn't get the garbage part you r mentioning !! – asn Mar 30 '18 at 12:04
  • 1
    You did not understand. The characters of the string will be packed, 1 byte each. But the total length will be multiple of 8. It will use as many qwords as needed to fit the text but the last one might not be fully used. See the nasm listing and the manual. – Jester Mar 30 '18 at 12:16
  • Learn at least to listen to @Jester. – zx485 Mar 30 '18 at 15:46
  • @Jester: there's already an SO answer with the appropriate quote from the NASM manual :P – Peter Cordes Mar 30 '18 at 18:12
  • @PeterCordes Can u provide the link down here ? – asn Mar 30 '18 at 18:14
  • I was talking about https://stackoverflow.com/questions/38860174/how-are-dw-and-dd-different-from-db-directives-for-strings, which this question is now closed as a duplicate of. I did that right before commenting. – Peter Cordes Mar 30 '18 at 18:23

0 Answers0