Loop exiting early when making Linux system calls [duplicate] - linux

This question already has answers here:
Why do x86-64 Linux system calls modify RCX, and what does the value mean?
(1 answer)
What are the calling conventions for UNIX & Linux system calls (and user-space functions) on i386 and x86-64
(4 answers)
Closed 5 years ago.
I am trying to write a program that prints an integer, the integer is stored in rax register, I don't know why it only prints the first digit of the integer, in this case it's 3.
The program divides rax by 10 and stores the remainder+48 in the reserved memory to display it later. we repeat this as long as rax is different than 0.
code:
section .bss
integer resb 100
section .text
global _start
_start:
mov rax, 321
call _printRAX
mov rax, 60
mov rdi, 0
syscall
_printRAX:
mov rbx, 10
mov rcx, integer
mov [rcx], rbx
_loop1:
inc rcx
mov rdx, 0
div rbx
add rdx, 48
mov [rcx], dl
cmp rax, 0
jne _loop1
_loop2:
mov rax, 1
mov rdi, 1
mov rsi, rcx
mov rdx, 1
syscall
dec rcx
cmp rcx, integer
jge _loop2
ret
output:
3
Try It Online

Related

ASM printf prints garbage/segfaults [duplicate]

This question already has answers here:
glibc scanf Segmentation faults when called from a function that doesn't align RSP
(1 answer)
What registers are preserved through a linux x86-64 function call
(3 answers)
Printing floating point numbers from x86-64 seems to require %rbp to be saved
(1 answer)
Why does printf still work with RAX lower than the number of FP args in XMM registers?
(1 answer)
Why does eax contain the number of vector parameters?
(1 answer)
Closed 6 days ago.
I have been trying to call printf from x64 asm, but it prints garbage
Here is the code:
;print all fib numbers from 0 until 100
global main
extern printf
;printf notes
;mov al 0 is neccesary
;rdi: format string
;rsi, rdx, r8... args
section .text
main:
mov r8, 0 ;counter
mov r9, 0 ;nth value
mov r10, 1;n + 1 value
mov al, 0
Loop:
mov rsi, r9
mov rdx, r10
mov rdi, format_str
call printf
mov r11, r10
add r10, r9
mov r9, r11
inc r8
cmp r8, 100
jl Loop
mov rax, 60
mov rdi, 0
syscall
section .rodata
format_str: db "%3d: %30d\\n", 0
Note the mov al. If it is inside the loop, the program prints garbage. But if it is outside the loop, then the program segfaults.
I compile with gcc and I simply want it to output as it would if it were called from c

NASM iterate over string and stop when finds certain value [duplicate]

This question already has answers here:
Garbage in string output function
(1 answer)
can't compare user input with number, nasm elf64
(2 answers)
Closed 3 years ago.
This question is similar to others, but it is not a duplicate. I am learning NASM (assembly).
My program has the following goal. I give as input a word (e.g "aab"). I want to compare each char in the string to a predefined value.
Here is a snippet.
;try to compare
xor rax,rax ;set iteration index to 0
lea rsi, [userpass]
back_to_loop:
cmp rsi, 'a'
je _exit
inc rsi ;increase index
jmp back_to_loop
!!!A good help would be editing the program in such a way, that the iteration would stop as soon as it finds an 'a' in the string. If I found how that is done, I could modify it to my own needs.
My entire effort so far:
section .text
global _start
_start:
;read input
xor rax, rax
mov rdi, rax
mov rsi, userpass
mov rdx, rax
add rdx, 0x64 ; 100
syscall
;try to compare
xor rax,rax ;set iteration index to 0
lea rsi, [userpass]
back_to_loop:
cmp rsi, 'a'
je _exit
inc rsi ;increase index
jmp back_to_loop
_exit:
mov rax, 1
xor rbx, rbx
int 80h
section .bss
num resb 64
section .data
userpass times 100 db 0 ;input

How to compare the count of command line arguments correctly in NASM?

I am learning x86_64 NASM assembly on Ubuntu 16.10 on Docker for Mac.
The following program takes two command line arguments, and sum these.
If number of command line arguments is not two, print error message (jump to argcError).
When I exec this program, it jump to argcError section despite passed to two command line arguments.
Why this program jump to argError?
section .data
SYS_WRITE equ 1
STD_IN equ 1
SYS_EXIT equ 60
EXIT_CODE equ 0
NEW_LINE db 0xa
WRONG_ARGC db "Must be two command line arguments", 0xa
section .text
global _start
_start:
pop rcx
cmp rcx, 3
jne argcError
add rsp, 8
pop rsi
call str_to_int
mov r10, rax
pop rsi
call str_to_int
mov r11, rax
add r10, r11
argcError:
mov rax, 1
mov rdi, 1
mov rsi, WRONG_ARGC
mov rdx, 35
syscall
jmp exit
str_to_int:
xor rax, rax
mov rcx, 10
next:
cmp [rsi], byte 0
je return_str
mov bl, [rsi]
sub bl, 48
mul rcx ; rax = rax * rcx
add rax, rbx
inc rsi
jmp next
return_str:
ret
int_to_str:
mov rdx, 0
mov rbx, 10
div rbx
add rdx, 48
add rdx, 0x0
push rdx
inc r12
cmp rax, 0x0
jne int_to_str
jmp print
print:
; calculate byte length of number string
mov rax, 1
mul r12
mov r12, 8
mul r12
mov rdx, rax
; print sum
mov rax, SYS_WRITE
mov rdi, STD_IN
mov rsi, rsp
syscall
jmp printNewline
printNewline:
mov rax, SYS_WRITE
mov rdi, STD_IN
mov rsi, NEW_LINE
mov rdx, 1
syscall
jmp exit
exit:
mov rax, SYS_EXIT
mov rdi, EXIT_CODE
syscall
There probably other errors in your code as pointed out by Micheal Petch, but the way you've initialized RSI is incorrect. Yes, ESP does point to the number of arguments passed, but popping it off the stack and then adding 8 to ESP again is functionally equivalent too.
mov rcx, [rsp]
Then by popping into RSI it only becomes a copy of RCX. If you want to do that it should look like this
pop rcx
.......
add rsp, 24 ; Now RSP is pointing to proper place in array of pointers
pop rsi
add rsp, 16 ; Now point to pointer to second argument
pop rsi
An alternative would be this next example only because my personal preference is not to use stack pointer for other than that which it was intended.
mov rsi, rsp
lodsq ; Read # of arguments passed by OS
add rsi, 8 ; bounce over application name
cmp al, 3
jnz argError
push rsi
lodsq
mov rsi, rax ; RSI points to first agument
call Convert
pop rsi
lodsq
mov rsi, rax
call Convert

Displaying a number in x86-64 assembly with only Linux system calls [duplicate]

This question already has answers here:
Why should EDX be 0 before using the DIV instruction?
(2 answers)
How do I print an integer in Assembly Level Programming without printf from the c library? (itoa, integer to decimal ASCII string)
(5 answers)
Closed 2 years ago.
Previous version of this question (that question originally had a different problem, even though the code there is now the same as the code in this question)
I am trying to make code to display a number on console in Linux 64 bit NASM, without the use of c/c++ functions (pure assembly). The code compiles and links fine but it will not give output...
It displays just newline for some time and then displays '7' forever. I am new to Assembly so I don't know what is wrong. Please help... Here is the code:
section .data
num: dq 102 ;my default number to get the reverse of (for now)
nl: db 0x0a
nlsize: equ $-nl
ten: dq 10
section .bss
rem: resq 1
remsize: equ $-rem
section .text
global _start
_start:
cmp qword [num], 0
jng _exit ;jump to _exit if num is not greater than 0
mov rax, [num] ;move the number to rax
mov rbx, [num] ;move the number to rbx as well so that i have original number in register to subtract and get the remainder
mov rcx, [ten] ;move 10 to rcx to be the divisor
div rcx ;divide number in rax by 10
mov [num], rax ;get the quotient to get the remaining number for quotient
mul rcx ;multiply number in rax by 10
sub rbx, rax ;subtract rbx - rax and store the value in rax (right?)
mov [rem], rbx ;get the remainder from rax. this must be done right after div (WHY??????????)
call _disprem ;call _disprem to display the remainder... call returns the flow back to the caller right?
jmp _start ;get to the loop again
_exit:
mov rax, 60
mov rdi, 0
syscall
_newl:
mov rax, 1
mov rdi, 1
mov rsi, nl
mov rdx, nlsize
syscall
ret
_disprem:
mov rax, 1
mov rdi, 1
add qword [rem], 0x0000000000000030 ;since the rem variable is quadword (64 bit)
mov rsi, rem ;for getting ascii value (48 is ascii 0 in decimal) to convert the rem to character
mov rdx, remsize
syscall
sub qword[rem], 0x0000000000000030 ;get me my original number back plz thanks
call _newl
ret

Displaying a number - assembly code not working Linux, x64 (NASM) [duplicate]

This question already has answers here:
Displaying a number in x86-64 assembly with only Linux system calls [duplicate]
How do I print an integer in Assembly Level Programming without printf from the c library? (itoa, integer to decimal ASCII string)
(5 answers)
Closed 2 years ago.
I am learning assembly on Linux (NASM) x64 machine (I don't have access to 32 or 16 bit machine), and I am trying to display number on screen (reverse of number according to code but that's a start).
Number is predefined in section .data -> num.
I am quite a newbie at assembly programming and due to the lack of material on x64 assembly (really, cant find much, and all I was able to find was quite confusing) I am unable to resolve the issue.
The issue is that the code compiles an links with no errors/warnings, but it just displays some spaces (not even newline). If I remove the call _newl code from _disprem, those spaces are also gone. There is not even segment fault or something.
By the way, algorithm to get the remainder (to get the digits in a number) is num - (num / 10) * 10
section .data
num: dq 102 ;my default number to get the reverse of (for now)
nl: db 0x0a
nlsize: equ $-nl
ten: dq 10
section .bss
rem: resq 1
remsize: equ $-rem
section .text
global _start
_start:
cmp qword [num], 0
jng _exit ;jump to _exit if num is not greater than 0
mov rax, [num] ;move the number to rax
mov rbx, [num] ;move the number to rbx as well so that i have original number in register to subtract and get the remainder
mov rcx, [ten] ;move 10 to rcx to be the divisor
div rcx ;divide number in rax by 10
mov [num], rax ;get the quotient to get the remaining number for quotient
mul rcx ;multiply number in rax by 10
sub rbx, rax ;subtract rbx - rax and store the value in rax (right?)
mov [rem], rbx ;get the remainder from rax. this must be done right after div (WHY??????????)
call _disprem ;call _disprem to display the remainder... call returns the flow back to the caller right?
jmp _start ;get to the loop again
_exit:
mov rax, 60
mov rdi, 0
syscall
_newl:
mov rax, 1
mov rdi, 1
mov rsi, nl
mov rdx, nlsize
syscall
ret
_disprem:
mov rax, 1
mov rdi, 1
add qword [rem], 0x0000000000000030 ;since the rem variable is quadword (64 bit)
mov rsi, rem
mov rdx, remsize
syscall
sub qword [rem], 0x0000000000000030 ;get me my original number back plz thanks
call _newl
ret

Resources