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
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
Using NASM, I need to change a character in a string at a given index and print the string in its new form. Here is a simplified version of my code:
;test_code.asm
global main
extern printf
output_str: db "----------"
index: dq 7
main:
push rbp
mov rdi, output_str
mov rax, index
mov byte[rdi + rax], 'x'
xor rax, rax
call printf
pop rbp
ret
I then compile using:
nasm -felf64 test_code.asm && gcc test_code.o -lm
and get a seg fault. Would someone please point out the flaw here? I can't seem to find it myself.
your string is in the .text section of the executable, which is read only by default. Either you allocate a buffer on the stack, copy the string and you modify it there, or you put the string in the .data section (which is read/write) using the section directive. In this last case, notice that the character replacement will be persistent, i.e. even later in the program the string will remain modified;
if you want to print that string with printf it has to be NUL-terminated. Add a ,0 to the end of the db line;
that mov rax, index is wrong - index is the address of the qword you wrote above, while you actually want to copy in rax the datum wrote there; you probably want mov rax, [index].
So, something like
;test_code.asm
global main
extern printf
section .data
output_str:
db "----------",0
section .text
index:
dq 7
main:
push rbp
mov rdi, output_str
mov rax, [index]
mov byte[rdi + rax], 'x'
xor rax, rax
call printf
pop rbp
ret
I'm writing a while loop in assembly to compile in the Linux terminal with nasm and gcc. The program compares x and y until y >= x and reports number of loops at the end. Here's the code:
segment .data
out1 db "It took ", 10, 0
out2 db "iterations to complete loop. That seems like a lot.", 10, 0
x db 10
y db 2
count db 0
segment .bss
segment .text
global main
extern printf
main:
mov eax, x
mov ebx, y
mov ecx, count
jmp lp ;jump to loop lp
lp:
cmp ebx, eax ;compare x and y
jge end ;jump to end if y >= x
inc eax ;add 1 to x
inc ebx ;add 2 to y
inc ebx
inc ecx ;add 1 to count
jp lp ;repeat loop
end:
push out1 ;print message part 1
call printf
push count ;print count
call printf
push out2 ;print message part 2
call printf
;mov edx, out1 ;
;call print_string ;
;
;mov edx, ecx ;these were other attempts to print
;call print_int ;using an included file
;
;mov edx, out2 ;
;call print_string ;
This is compiled and run in the terminal with:
nasm -f elf test.asm
gcc -o test test.o
./test
Terminal output comes out as:
It took
iterations to complete loop. That seems like a lot.
Segmentation fault (core dumped)
I can't see anything wrong with the logic. I think it's syntactical but we've only just started learning assembly and I've tried all sorts of different syntax like brackets around variables and using ret at the end of a segment, but nothing seems to work. I've also searched for segmentation faults but I haven't found anything really helpful. Any help would be appreciated because I'm an absolute beginner.
The reason it crashes is probably that your main function doesn't have a ret instruction. Also be sure to set eax to 0 to signal success:
xor eax, eax ; or `mov eax, 0` if you're more comfortable with that
ret
Additionally, global variables designate pointers, not values. mov eax, x sets eax to the address of x. You need to write back to it if you want anything to happen (or not use global variables).
Finally, you're calling printf with a single non-string argument:
push count ;print count
call printf
The first argument needs to be a format string, like "%i". Here, count is a pointer to a null byte, so you get nothing instead. Off my head, you should try this:
out3 db "%i ", 0
; snip
push ecx
push out3
call printf
I think your problem might just be that you are referencing the addresses of your constants and not their intrinsic value. One must think of a label in nasm as a pointer rather than a value. To access it you just need to use [label]:
segment .data
x dw 42
segment .text
global main
extern printf
main:
mov eax, x
push eax
call printf ; will print address of x (like doing cout<<&x in C++)
mov eax, [x]
push eax
call printf ; will print 42
sub esp, 8
xor eax, eax
ret
PS:I don't think anyone has mentioned it but volatile registers are modified very often when calling external code (C or C++ or other) since at compilation those functions you use are "translated" to assembly and then linked with your asm file. The PC is not a human so it is not distinguishing between what was written in high-level or low-level, the processor is just reading opcodes and operands stored in registers and memory, hence why an external function when using low-level language (call printf) is going to modify (or not! always depends on compiler and architecture) registers that you are also using.
To solve this there are various solutions:
You check what registers are not being modified by using gcc your_c_file.c -S and then in the file your_c_file.swill be the pre-prepared assembly code your compiler has produced from your C file. (It tends to be quite hard to figure out what is what and if you are going to use this method check out Name Mangling, to see how func names will be changed.)
Push all the registers you want to save to stack, and then after the call pop them back to their registers keeping in mind LIFO method.
Use the instructions PUSHA and POPAwhich push or pop all registers respectively.
This is the NASM manual chapter 3 which explains the basis of the language to use: http://www.csie.ntu.edu.tw/~comp03/nasm/nasmdoc3.html
Hope you managed to solve it.
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.
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.