0

this is the code that runs properly in NASM-v2.13.02 properly...

section .data
    digit db 0,10;defines the first number with newline character
    
section .text
    global _start
    
_start:
    mov rbx, 48             ;ascii 48 => '0'
    mov rcx, 57             ;ascii 57 => '9'
    
    loop:   call _printRBX  ;print rbx
        add rbx, 1          ;rbx += 1
        cmp rbx, 57         ;rbx <= 57 ? (true/false)
    jle loop                ; jump to "loop" if above is true
    
    mov rax, 60             ;id=sysexit
    mov rdi, 0              ;errorcode=0
    syscall
    
_printRBX:          ;prints last byte of rbx
    mov [digit],bl  ;move pointer of digits to last 1byte of rbx
    mov rax, 1      ;id=syswrite
    mov rdi, 1      ;desc=standard output
    mov rsi, digit  ;buffer input
    mov rdx, 2      ;write byte size
    syscall
    ret

While the moment "57" in label "loop" is replaced with rcx...it becomes an infinite loop!!

section .data
    digit db 0,10;defines the first number with newline character
    
section .text
    global _start
    
_start:
    mov rbx, 48             ;ascii 48 => '0'
    mov rcx, 57             ;ascii 57 => '9'
    
    loop:   call _printRBX  ;print rbx
        add rbx, 1          ;rbx += 1
        cmp rbx, rcx        ;rbx <= rcx ? (true/false)
    jle loop                ; jump to "loop" if above is true
    
    mov rax, 60             ;id=sysexit
    mov rdi, 0              ;errorcode=0
    syscall
    
_printRBX:          ;prints last byte of rbx
    mov [digit],bl  ;move pointer of digits to last 1byte of rbx
    mov rax, 1      ;id=syswrite
    mov rdi, 1      ;desc=standard output
    mov rsi, digit  ;buffer input
    mov rdx, 2      ;write byte size
    syscall
    ret

Please let me understand how this happens and how can I resolve this!! Thanks!

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • Single step the program and see where `rcx` is changed from what you expect it to have. – Erik Eidt Oct 15 '20 at 16:51
  • @ErikEidt how to do so??am sorry--this is my second assemby program right after "Hello World" – Robert Kashyap Oct 15 '20 at 16:54
  • 1
    https://ncona.com/2019/12/debugging-assembly-with-gdb/ is a start. – Nate Eldredge Oct 15 '20 at 17:28
  • @NateEldredge thanks.... I'll can now see how it works – Robert Kashyap Oct 15 '20 at 21:04
  • @petercordes Thanks a lottt!!! The above tags answer my issue... syscalls have been using RCX and R11 to store essential data for "address of next instruction" and "flags" values respectively....Due this the syscalls were playing with RCX values.... gdb helped a lot too... Thanks mates! Ref: https://stackoverflow.com/questions/47983371/why-do-x86-64-linux-system-calls-modify-rcx-and-what-does-the-value-mean and also Ref: https://stackoverflow.com/questions/50571275/why-does-a-syscall-clobber-rcx – Robert Kashyap Oct 16 '20 at 04:07

0 Answers0