Print values of variables on a line (without new line) [duplicate] - linux

This question already has answers here:
Printf without newline in assembly
(5 answers)
Printing an entire array on a single line using printf asm nasm
(3 answers)
Closed 2 years ago.
So, i faced with a problem, that print each value of variable on a new line, but format for print is a line. I really spend a lot of time that find solution. But this solution don't work. I think that is because i am noob in assembler. So, i deleted all my fail... This is my code:
global main
extern printf
section .data
str: db "%i", 10, 0
count: dw 1
section .text
main:
start:
push dword [count]
push dword str
call printf
push dword [count]
push dword str
call printf
So, now in terminal printed this:
1
1
What do I need to do to print like this:
1 1
Please, example me code, that i understand.... Please, don't send me first link in internet with similar problem, because i tried this. I really don't understand...
How i can insert in print string with differents char, for example ':', 'space', '/' and other...?
Thanks you :)

Change
str: db "%i", 10, 0
to
str: db "%i ", 0
10 is a newline character. This replaces it with a space.

Related

how to reformat an integer to be printed as a string using puts (AT&T, x86_64) [duplicate]

This question already has answers here:
Printing an integer as a string with AT&T syntax, with Linux system calls instead of printf
(2 answers)
How to load address of function or label into register
(1 answer)
Closed 2 years ago.
Working on an assembly project, I'm trying to use "puts" to print an integer saved in a temporary variable. I'm aware that the integer has to be reformatted to a string format before it can be printed and my question is how to do that.
Using AT&T syntax.
for example:
.data
temp: .quad 0
outbuf: .space 64
.global main
main:
movq $5, temp
reformat:
// the function to reformat the temp variable and move it to outbuf
printBuf:
movq outbuf, %rdi
call puts
ret

Segmentation fault if I try to print a string char by char [duplicate]

This question already has answers here:
What registers to save in the ARM C calling convention?
(6 answers)
Printf Change values in registers, ARM Assembly
(2 answers)
Closed 2 years ago.
I am trying to print a string char by char, with this ARM32 code:
.global main
.type main%function
# r0 = asciz c
# r1 = singlechar
# r2 = string
# r3 = offset
main:
mov r3,#0 // initialize offset
ldr r0,=single_c
ldr r2,=string
push {ip,lr} // save the lr
loop:
ldrb r1,[r2,r3] // load 1 byte of the address string+offset
cmp r1,#0 // if the char is the null char
beq end // then go to the end
bl printf // else printf("%c",r1)
add r3,#1 // increase offset
b loop // repeat the loop
end:
pop {ip,lr} // restore lr
bx lr // return
string:
.asciz "Test\n"
single_c:
.asciz "%c\n"
I really don't get what am I doing wrong, since if I execute it, I get this:
$ /a.out
T
Segmentation Fault
So it only prints the first letter.
EDIT:
I added a push {r0,r1,r2,r3} before the printf call and a pop {r0,r1,r2,r3} after the printf call, and now it works... but my question is: does printf ALWAYS modify the first four register?

How do I count the number of characters in a string in NASM?

I want to take an input and display the input with how many characters are in the string.
So far I'm able to produce the string, but I'm confused as to how I get the input?
You can use this function:
count db 0; (or resb 1), this is the place where will stay the result
count_string:
lodsb; // load char( letter )
cmp al, 0x00; // check string end; cause 0x00 its not a letter or number, 0 is 30h in ascii
jz done; // count done
inc byte [count]; adds 1 to count register
jmp count_string; // check next char
done:
ret; // exit function
And you can define a string this way:
data:
; or in the section .data
string DB "This is my string", 0; 0 means the end of the string
you can call the function this way:
mov si, string; move the string pointer to register
call count_string;
and if you are using nasm inside a OS like windows or ubuntu uou can watch this: https://www.youtube.com/watch?v=VAy4FGHDx1I
but in x86 i did a 4 byte( for letters ) command line, its really easy to get much more but it was just a quick experiment i used int 16h( keyboard )

Limit the characters of a variable? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have many variables that will be called in a loop. I wanted, for example - To make the variable, when exceeding 30 characters, it would be cut to 30 characters and add 3 dots at the end.
Example:
set text = Hello world
Becomes:
"Hello ..."
That would help me a lot, however - If anyone can do something more advanced, for example when cutting some parts of characters, add the dots to the middle of the text.
Example:
set text = Hello wooooooooooooooooooooooooooooooooorld "
Becomes:
"Hello wo...orld"
#echo off
call :convert Hello
call :convert Hello beautiful World
call :convert Hello wooooooooooooooooooooooooooooooooorld
goto :eof
:convert
set "x=%*"
REM if the string is shorter than 10 chars, just print it and return:
if "%x:~10%" == "" echo %1 & goto :eof
REM else print first 7 chars, thee dots and the last three chars:
echo %x:~0,7%...%x:~-3%
Adapt the following numbers to your needs (you mentioned 30 chars, but none of your examples do match that number):
10 for "first ten characters" (due to zero-based counting it checks, if there is anything in the eleventh position)
7 for the number of characters before the three dots
3 for the number of last characters
The answer to your issue can be solved very easily using %variable:~num_chars_to_skip%
Using the SET command we can edit your variable and have it remove everything over 30 characters in length. From there we can take the new variable we made and manipulate it from there.
This code bellow will remove all characters exceeding 30 and display them in the text... format.
#ECHO OFF
::Edit string one with 30 char limit. String has 40 Chars.
SET String=0123456789012345678901234567890123456789
SET Result=%String:~0,30%
ECHO %Result%...
::Edit string one with 30 char limit. String has 90 Chars.
SET String=012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
SET Result=%String:~0,30%
ECHO %Result%...
::Edit string one with 30 char limit. String has 6 Chars.
SET String=0123456
SET Result=%String:~0,30%
ECHO %Result%...
PAUSE
GOTO :EOF
Learn more about syntax-substring here: https://ss64.com/nt/syntax-substring.html

converting a decimal/hex value in a register to ascii

So say I have this value in a register ebx: 30303420
I want to convert that and print out the corresponding ascii values. So it SHOULD print out
004
30 == 0
30 == 0
34 == 4
20 == space character.
How would I get that to print on the screen?
This is 80x86 architecture, using assembly code.
Well, your question have a couple unclear details.
1- If you have 30303420 Hex value in ebx, then you have 4 Ascii characters, precisely "004 ", that is:
mov ebx,30303420H ;is exactly the same than:
mov ebx,"004 "
You have NOT any decimal value (wich one?), so there is not any conversion here.
2- If you want to show that ebx value in the screen, so it shows "004 ", then you must specify under which operating system your program will run in order to use the appropiate services. For example, if you want to use old-style MS-DOS INT 21H functions, that also run in a DOS Window in Windows, then this segment do that:
mov cx,4 ;counter = 4 characters
;
next:
rol  ebx,8 ;rotate left EBX 1 byte: place next char in BL
mov dl,bl ;DL = char to show
mov ah,2 ;AH = VIDEO OUTPUT function
int 21H  ;DOS kernel service Int: show the char
loop next ;repeat 4 times
However, if your program run under Linux, the method to show ebx value is entirely different. Also, your program may use a C library function in a different way, or be a Windows-compliant program, or use BIOS INT 10H service (in charge of the screen), or even directly access the video circuitry, etc, etc, etc...

Resources