I am using NASM for compiling my ASM program and I am having trouble figuring out how to print an entire array on a single line (without necessarily knowing how big the array is) using a loop. Whenever I create a loop with printf it prints the values on multiple lines instead of one line. Any idea how to make printf print multiple values of an array on a single line using a loop? I get values 1-9 but all on a different line instead of the same line. This is to be done without using external libraries other than: the printf c library. Any help would be most appreciated. The code I have is below.
extern printf
SECTION .data ; Data section, initialized variables
array: dd 1, 2, 3, 4, 5, 6, 7, 8, 9, 0; this is a test array for testing purposes
arrayLen: dd 9 ; length of array
aoutput: db "%d", 10, 0 ; output format
SECTION .text ; Code section.
global main ; the standard gcc entry point
main: ; the program label for the entry point
push ebp ; set up stack frame
mov ebp,esp
mov ecx, [arrayLen] ; loop counter set up
mov esi, 0 ; counter to increment set up for looping through array
.loop:
push ecx ; make sure to put ecx (counter) on stack so we don't lose it when calling printf)
push dword [array + esi] ; put the value of the array at this (esi) index on the stack to be used by printf
push dword aoutput ; put the array output format on the stack for printf to use
call printf ; call the printf command
add esp, 8 ; add 4 bytes * 2
pop ecx ; get ecx back
add esi, 4
loop .loop
mov esp, ebp ; takedown stack frame
pop ebp ; same as "leave" op
mov eax,0 ; normal, no error, return value
ret ; return
The only thing that determines whether something is printed on a single line or multiple, is if you print a newline character (\n) or not.
Here, when you say 10, that is the ASCII value for a Line Feed. If you change this:
aoutput: db "%d", 10, 0
to this:
aoutput: db "%d ", 0
your values will be separated by spaces, instead of by new lines.
After the final value in the sequence, you can print a lone newline character:
push 0x0A ; New line character
call putchar
I figured out this (which is the same as the other answer, I just found it out before I saw it was posted).
I figured it out, I changed the format definition to:
aoutput: db "%d ", 0 ; output format
from:
aoutput: db "%d ", 10, 0 ; output format
and just added an newline format to add a new line at the end.
newline: db "", 10, 0 ; newline format
The problem is that you are outputting a line feeds after each element.
What follows are two solution. Neither of them suffer from the problem of leaving a trailing blank space as the two existing solutions do.
Note that I made a slight rearrangement of the loop to allow empty arrays. Allowing empty arrays is a good practice even if it's not necessary since it doesn't require any extra instructions.
Solution 1: Use "%d\n" as the format if it's the last element or "%d " otherwise.
This is good if you don't want to print the line feed for empty arrays.
format1: db "%d ", 0
format2: db "%d", 10, 0
jmp .loop3
.loop1:
mov eax,format1
cmp ecx,1
jnz .loop2
mov eax,format2
.loop2
push ecx
push dword [array + esi]
push dword eax
call printf
add esp, 8
pop ecx
add esi, 4
.loop3:
loop .loop1
Solution 2: Use "%d" as the format if it's the first element. Use " %d" as the format otherwise. Print a line feed after the loop.
This is good if you want to print the line feed even for empty arrays.
format1: db "%d", 0
format2: db " %d", 0
mov eax, format1
jmp .loop2
.loop1:
push ecx
push dword [array + esi]
push dword eax
call printf
add esp, 8
pop ecx
add esi, 4
mov eax, format2
.loop2:
loop .loop1
push 10
call putchar
Pardon any errors; the x86 didn't have an eax register the last time I wrote assembly for it. The logic should be evident in any case.
Related
I have some code in assembly which behaves a little bit strange. I have a C extern function that calls with asm another function from an .asm file. This C function puts on the stack three addresses used by my function from .asm file. All went well untill this appeared:
; Let's say we take from the stack first parameter from my C function.
; This parameter is a string of bytes that respect this format:
; - first 4 bytes are the sign representation of a big number
; - second 4 bytes are the length representation of a big number
; - following bytes are the actual big number
section .data
operand1 dd 0
section .text
global main
main:
push ebp
mov ebp, esp
mov eax, [ebp + 8] ; Here eax will contain the address where my big number begins.
lea eax, [eax + 8] ; Here eax will contain the address where
; my actual big number begins.
mov [operand1], eax
PRINT_STRING "[eax] is: "
PRINT_HEX 1, [eax] ; a SASM macro which prints a byte as HEX
NEWLINE
PRINT_STRING "[operand1] is: "
PRINT_HEX 1, [operand1]
NEWLINE
leave
ret
When running this code, I get at the terminal the correct output for [eax], and for [operand1] it keeps printing a number which will not change if I modify that first parameter of my C function. What am I doing wrong here?
I made an understandable mistake. When doing:
mov [operand1], eax
PRINT_STRING "[operand1] is: "
PRINT_HEX 1, [operand1]
NEWLINE
This code prints the first byte of the content (which is the address where my actual big number begins) contained at the address where this local variable (operand1) resides. In order to get the actual value which resides at [operand1] I had to do this:
mov ebx, [operand1]
PRINT_STRING "[operand1] is: "
PRINT_HEX 1, [ebx]
NEWLINE
I'm new to Assembly, I need help with an assignment in Assembly Language Irvine32. I want to know where I'm going wrong. I believe my code is 80% right but there's something I'm not seeing or recognizing. Here's the program details.
"Write an assembly language program that has an array of words. The program loads the last element of the array into an appropriately sized register and prints it. (Do not hardcode the index of the last element.)"
INCLUDE Irvine32.inc
.data
val1 word 1,2,3,4,5,6
val2 = ($-val1)/2
.code
main PROC
mov ax, 0
mov ax, val1[val2]
Call WriteDec
Call DumpRegs
exit
main ENDP
END main
First of all, your code has a bug: val1[val2] indexes with the element count in words, not the length in bytes (unless MASM syntax is even more magical than I expect). And it reads from one past the end of the array, since the first element is at val1[0].
To find the end, you either need to know the length (explicit length, like a buffer passed to memcpy(3)), or search it for a sentinel element (implicit length, like a C string passed to strcpy(3)).
Having a function that accepts an explicit length as a parameter seems fine to me. It's obviously much more efficient than a loop scanning for a sentinel element, and the array shown doesn't include one. (See Jose's answer for a suggestion to use '$' (i.e. 36) as a sentinel value. -1 or 0 might be more sensible sentinels/terminators.)
Obviously knowing the length is much better, since there's no need for a loop scanning the whole array.
I'd only call it hard-coding if you wrote val2 = 6, or worse val2 dw 6, rather than having it calculated at assemble-time from the array. If you want to write a function that could work with non-compile-time-constant arrays, you can have it accept the length as a value in memory, instead of an immediate that will be embedded into its load instruction.
e.g.
Length as a parameter in memory
.data
array word 1,2,3,4,5,6
array_len word ($-array)/2 ; some assemblers have syntactic sugar to calc this for you, like a SIZE operator or something.
.code
main PROC ; inputs: array and array_len in static storage
; output: ax = last element of array
; clobbers: si
; mov ax, 0 ; This is useless, the next mov overwrites it.
mov si, [array_len] ; do we need to save/restore si with push/pop in this ABI?
add si,si ; multiply by 2: length in words -> length in bytes
mov ax, [array + si - 2] ; note that the -2 folds into array at assemble time, so it's just a disp16 + index addressing mode
Call WriteDec
Call DumpRegs
exit
main ENDP
END main
You could also write a function to take pointer and length args on the stack or in registers, and have main pass those args.
You could save the add (or shl) by accepting a length in bytes, or a start and one-past-the-end pointer (like C++ STL range functions that take .begin() and .end() iterators). If you have the end pointer, you don't need the start pointer at all, except to return an error if they're equal (size = 0).
Or if you were not stuck with obsolete 16bit code, you could use a scaled index in the addressing mode, like [array + esi * 2]. You include Irvine32.inc...
I think your solution to reach the last element is the most efficient (($-val1)/2), but #zx485 is right and your teacher might believe you are cheating, so, among other solutions, you can reach the last element with a loop and the pointer SI :
INCLUDE Irvine32.inc
.data
val1 word 1,2,3,4,5,6
val2 = ($-val1)/2
.code
main PROC
; mov ax, 0
; mov ax, val1[val2]
mov cx, val2-1 ;COUNTER FOR LOOP (LENGTH-1).
mov si, offset val1 ;SI POINTS TO FIRST WORD IN ARRAY.
repeat:
add si, 2 ;POINT TO NEXT WORD IN ARRAY.
loop repeat ;CX--, IF CX > 0 REPEAT.
mov ax, [ si ] ;LAST WORD!
Call WriteDec
Call DumpRegs
exit
main ENDP
END main
One shorter way would be to get rid of the loop and jump straight to the last element by using the SI pointer (and changing val2 just a little) :
INCLUDE Irvine32.inc
.data
val1 dw 1,2,3,4,5,6
val2 = ($-val1)-2 ;NOW WE GET LENGTH - 2 BYTES.
.code
main PROC
; mov ax, 0
; mov ax, val1[val2]
mov si, offset val1 ;SI POINTS TO FIRST WORD IN ARRAY.
add si, val2 ;SI POINTS TO THE LAST WORD.
mov ax, [ si ] ;LAST WORD!
Call WriteDec
Call DumpRegs
exit
main ENDP
END main
And "Yes", you can join those two lines :
mov si, offset val1 ;SI POINTS TO FIRST WORD IN ARRAY.
add si, val2 ;SI POINTS TO THE LAST WORD.
into one, I separated them to comment each other :
mov si, offset val1 + val2
If you cannot use val2 = ($-val1)/2, one option would be to choose some terminating character for the array, for example, '$', and loop until it's found:
INCLUDE Irvine32.inc
.data
val1 word 1,2,3,4,5,6,'$' ;ARRAY WITH TERMINATING CHARACTER.
;val2 = ($-val1)/2
.code
main PROC
;mov ax, 0
;mov ax, val1[val2]
mov si, offset val1 ;SI POINTS TO VAL1.
mov ax, '$' ;TERMINATING CHARACTER.
repeat:
cmp [ si ], ax
je dollar_found ;IF [ SI ] == '$'
add si, 2 ;NEXT WORD IN ARRAY.
jmp repeat
dollar_found:
sub si, 2 ;PREVIOUS WORD.
mov ax, [ si ] ;FINAL WORD!
Call WriteDec
Call DumpRegs
exit
main ENDP
END main
My question: Why is it printing twice when I'm making one call to printf?
Note that yes, I'm aware I'm allocating space in heap for a variable stored on the stack. I'm only doing this to get used to malloc, pointers, and 'arrays' in NASM.
Compiling in x64 bit machine with:
nasm -f elf32 -o TEMP.o file.asm
and:
gcc -m32 -o exec TEMP.o
extern exit, printf, malloc, free
global main
section .data
format db "%s", 10
msg: db "Hello!!",10
BUF equ $-msg + 1
section .text
main:
push BUF ; How many bytes do we want to allocate
call malloc ; ptr stored in EAX
add esp, 4 ; clear the last thing on the stack (BUF)
mov esi, eax ; new source index at malloc pointer
xor ecx, ecx ; clear ECX (counter for us)
loop:
mov dl, [msg+ecx] ; mov letter into dl
mov BYTE [esi+ecx], dl ; cat dl onto array
inc ecx ; add 1 to our ounter
cmp ecx, BUF-1d
jl loop
xor edx, edx
mov BYTE [esi+ecx], dl
add esp, 4
mov esi, eax
push esi ;
push format
call printf
add esp, 4*2
push esi
call free
push 0
call exit
add esp, 4
You only need to end your strings with 0
format db "%s", 10, 0
msg: db "Hello!!",10 ,0
Okay i'v read the comments just now and see you said you had code that is meant to insert the 0s, i'd check that, because i copied/pasted your code and only added the 0s on the ends of the strings to make it output one string, i didn't even notice the insertion code let alone touch it, but i can only assume that is where your problem is.
Okay, long story short, I'm learning assembly, and I'm trying to make a loop print out the ascii characters "0" - "9".
So, I did all of the basics I've been seeing in examples, like saving register states with pushad and popad, allocating stack space, and making sure I leave things the way they started.
So I managed this small example:
;
; Hello_World.asm
; (NASM Syntax, Windows)
extern _printf
section .text
_main:
pushad ; save register states
push ebp ; save old stack
mov ebp, esp ; prepare new stack
sub esp, 1*4 ; allocate 4 bytes
mov byte [esp + 0], 48 ; add ascii '0' to stack
mov byte [esp + 1], 0 ; add ascii NULL terminator to stack
push esp; ; push the string in the stacks refrence
call _printf ; call printf()
add esp, 4 ; pop string refrence
add esp, 1*4 ; deallocate 4 bytes
mov esp, ebp ; close this stack
pop ebp ; restore old stack
popad ; restore register states
ret ; leave this function
This works, it prints out '0', but that's a little on the safe side. I tried adding a loop into it, but things just fall apart there. I read that the 'loop' opcode is supposed to decrement the ECX register, and go back to the label parameter should ECX > 0, however, I don't think I've quite got it yet.
So I add a few lines, and come up with this:
;
; Hello_World.asm
;
extern _printf
global _main
section .text
_main:
pushad ; save register states
push ebp ; save old stack
mov ebp, esp ; prepare new stack
sub esp, 1*4 ; allocate 4 bytes
mov byte [esp + 0], 48 ; add ascii '0' to stack
mov byte [esp + 1], 0 ; add ascii NULL terminator to stack
mov ecx, 9 ; set loop counter to 9
aLoop:
inc byte [esp + 0] ; increment ascii character
push esp; ; push the string in the stacks refrence
call _printf ; call printf()
add esp, 4 ; pop string refrence
loop aLoop ; loop back to aLoop if ecx > 0
add esp, 1*4 ; deallocate 4 bytes
mov esp, ebp ; close this stack
pop ebp ; restore old stack
popad ; restore register states
ret ; leave this function
Well, now things go crazy. I run it in command prompt and I hear this beeping through my headphones, and it's cycling through every ascii character, printing them all out. So after about 5 seconds of flying characters, I assume something overflows, and it just crashes.
I'm pretty new to assembly (today's my first day of real coding), and I don't see what's going wrong. Could someone please explain how I could better implement a loop ?
Thanks ahead!
-Jason
Does the "_printf" subroutine preserve the contents of ECX? If not, that could be your problem. Try saving it across the call.
okay, well, what you're describing is that the loop isn't terminating for some reason. That means the problem pretty well has to be here:
add esp, 4 ; pop string refrence
loop aLoop ; loop back to aLoop if ecx > 0
This sounds like a job for a debugger: what's really happening to ecx?
Well, I note that you're setting ecx to 9. You're then adding 4 to esp. When are you changing ecx? (Yes, I know it's supposed to be happening in the loop instruction, but if that were working you wouldn't be asking. What's really happening to ecx?)
The beeping, by the way, is easy: as you cycle through all the ASCII characters, you're hitting ASCII 0x07, the BEL character.
aLoop:
inc byte [esp + 0] ; increment ascii character
push ecx; ; save ecx
push esp; ; push the string in the stacks refrence
call printf ; call printf()
add esp, 4 ; pop string refrence
pop ecx
loop aLoop ; loop back to aLoop if ecx > 0
The caller-saved registers are eax, ecx, edx. The called subroutine is allowed to modify these registers. Look for caller-saved vs callee-saved registers using a search engine. Should give you more details.
I need to translate this C code to assembly language code
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int answer, i;
int right, wrong;
right = 0;
wrong = 0;
for(i =1; i < 11; i = i+1){
printf("What is %d + %d? ", i,i);
scanf( "%d", &answer);
if(answer == i + 1) {
printf("right! ");
right++;
}
else {
printf("Sorry, you're wrong. ");
printf("The answer is %d. ", i + 1);
wrong++;
}
}
printf("You got %d right and %d wrong. ", right, wrong );
return 0;
}
I really just need to know how to combine a variable with a string in assembly language like in the above C code. I think I can handle everything else. Could somebody tell me. Would I have to use some kind of reference[].
Note I'm using MASM and working out of Kip Irvine's Assembly Language for x86 processors 6th edition book
update heres the code I attempted to write over to MASM from one of the answerer's answer I keep getting a error. Like I said before I'm using Kip Irvine's Assembly Language so I have to include the library link INCLUDE Irvine32.inc
this is the error>>>> programb.obj : error LNK2019: unresolved external symbol _scanf referenced in function _main#0
INCLUDE Irvine32.inc
can somebody help me get this right
.data
string1 db "What is %d + %d? ", 0
string2 db "%d", 0
string3 db "right! ", 0
string4 db "Sorry, you're wrong. The answer is %d", 10, 0
string5 db "You got %d right and %d wrong.", 10, 0
answer dd 0
right dd 0
wrong dd 0
.code
main PROC
mov ebx, 1
L1:
cmp ebx, 11
je L2
push 1
push ebx
mov edx,OFFSET string1
call WriteString
add esp, 12
push answer
mov edx,OFFSET string2
call scanf
add esp, 8
inc ebx
cmp answer, ebx
jne L3
push ebx
mov edx,OFFSET string3
call WriteString
add esp, 8
inc right
jmp L1
L3:
push ebx
mov edx,OFFSET string4
call WriteString
add esp, 8
inc wrong
jmp L1
L2:
push wrong
push right
mov EDX,OFFSET string5
call WriteString
add esp, 12
exit
main ENDP
END main
programb.obj : error LNK2019: unresolved external symbol _scanf referenced in function _main#0
I'm sorry about the assembly language code....I don't know how to format it so it can be easier to read....
You can use the -S flag to gcc to produce the assembly code:
gcc myfile.c -S -o myfile.s
What I mean is that this assembly file should answer all your questions.
I was bored so I did this for you. I used NASM, rather than MASM. I assumed that EBX is a callee-saved register. The code is not particularly good. :-)
section .data
answer: dd 0
right: dd 0
wrong: dd 0
section .text
extern printf
extern scanf
global main
main:
push ebx
mov ebx, 1
.loop_start:
cmp ebx, 11
je .loop_end
push 1
push ebx
push .string0
call printf
add esp, 12
push answer
push .string1
call scanf
add esp, 8
inc ebx
cmp dword [answer], ebx
jne .wrong
push ebx
push .string2
call printf
add esp, 8
inc dword [right]
jmp .loop_start
.wrong:
push ebx
push .string3
call printf
add esp, 8
inc dword [wrong]
jmp .loop_start
.loop_end:
push dword [wrong]
push dword [right]
push .string4
call printf
add esp, 12
pop ebx
xor eax, eax
ret
section .data
.string0:
db "What is %d + %d? ", 0
.string1:
db "%d", 0
.string2:
db "right! ", 0
.string3:
db "Sorry, you're wrong. The answer is %d", 10, 0
.string4:
db "You got %d right and %d wrong.", 10, 0
Luckily for you, the printf function will do almost everything for you, even from assembly. You've probably read about the stack, and how you can call functions that take arguments that have been pushed on the stack. The same is true of printf. Push on the arguments in reverse order, so that the top thing on the stack is a reference to the format string. Then all you have to do is:
call printf
If I remember correctly, printf knows it has at least one argument, and that first argument (the format string) is the one that the stack pointer is pointing to. So then printf will scan through the format string and check if it needs to substitute in any of your other arguments, like i and i+1. Again, printf's doing this, you don't need to worry about it.
Hope this helps!
P.S. Re: the previous answers, usually it's not helpful to look at compiler-generated assembly code if you're trying to learn assembly. Even without optimizations enabled, the output's not meant for humans to read.
As an example , this line in C programming language:
printf("\n%d%s%d\n\n",num1," is not equal to ",num2);
is equivalent to:
printf PROTO arg1:Ptr Byte, printlist:VARARG
.data
msg1fmt byte 0Ah,"%d%s%d",0Ah,0Ah,0
msg1 byte " is not equal to ",0
num1 sdword 5
num2 sdword 7
.code
main proc
INVOKE printf, ADDR msg1fmt, num1, ADDR msg1, num2
ret