My homework says:
Write an assembly language program that, when given an integer n, will calculate the value of the expression (2n^2-1)2 + n^3. Your program is to get the value for n from ECX and is to leave the value of the expression in EAX. (Note: The name of the integer multiplication instruction is imul.)


This is what I have:

;;

;; Register usage:
;;      EAX     Sum
;;      ECX     Original value of n
;;      K     Placeholder for n
;;      D     Placeholder for n

[SECTION .text]

        global  main

main:
        push    EBP
        mov     EBP, ESP
        push    EBX
        push    ESI
        push    EDI

first:
        add     ECX, 0
        add     EAX, 0
        
var     K, D: integer;
top:
        K := 0;
        D :=0;

        mov     K, ECX
        imul    K, K
        imul    K, 2
        sub     K, 1
        imul    K, K
        mov     D, ECX
        imul    ECX, ECX
        imul    ECX, D
        add     ECX, K
        mov     EAX, ECX
last:
        pop     EDI
        pop     ESI
        pop     EBX
        mov     ESP, EBP
        pop     EBP
        ret

I dont think Im assigning variables correctly and I have no clue how to...I believe I have the right arithmetic but cant get past declaring variables.

hw1_1.asm:26: error: parser: instruction expected
hw1_1.asm:28: error: parser: instruction expected
hw1_1.asm:29: error: parser: instruction expected

Please help, I am so lost in assembly, Im used to java

Edited 5 Years Ago by rcossy1023: n/a

Hi,
You need a complete course on assembly language.
You try to declare a variable inside the text section. Text section is for machine instructions.
If you must reserve space for variables, you can achieve it in:
data section if the variable is initialized.
bss section if the variable in not initialized.
In this case data will reside in the bss section. Do reserve space in the bss section, nasm have this method:

[section .bss]
	 K : resd 1
	 D : resd 1

before the text section
The 'd' means dword, four bytes.
In assembly language there is not a ':=' operator. If you need to assign a value, you can do it like 'mov eax, ebx'. It will copy the value inside ebx into eax.
Another big error is in the imul operands. imul multiplies with sign the operand by eax and put the result in edx:eax.

mov edx, 0
mov eax, 2
mov ebx, 3
imul ebx

edx contains 0 and eax contains 6.
A bigger error is:

sub K, 1

In x86 it is not possible to operate with two places in memory. The assembler will complain with a message: invalid combination of opcode and operands.
You must write something like this:

mov eax, K
sub eax, 1
mov K, eax

And, finally, do you wish to try this code - with nasm syntax-:

;;
	;; Register usage:
	;; EAX Sum
	;; ECX Original value of n
	

[SECTION .text]
	global main     
	
main:
	push EBP
	mov EBP, ESP

	push EBX
	push	edi
	    
	;eax := n
	mov	eax,	ecx
	
	;eax := n ^ 2
	imul	eax

	;eax := (n ^ 2) * 2	
	mov	ebx,	2
	imul	ebx

	;eax := ((n ^ 2) * 2) - 1
	dec	eax

	;eax := (((n ^2 ) * 2) - 1) * 2
	imul	ebx

	;edi := (((n ^2 ) * 2) - 1) * 2
	mov	edi,	eax	

	;eax := n
	mov	eax,	ecx

	;ebx := n
	mov	ebx,	ecx

	;eax := n ^ 2
	imul	ebx

	;eax := n ^ 3
	imul	ebx

	;eax := (n ^ 3) + ((((n ^ 2) * 2) - 1) * 2)
	add	eax,	edi

	pop	edi
	pop EBX
	
	mov ESP, EBP
	pop EBP
	
	ret

I hope that this can be useful.

Thanks! I actually worked on it some more and got it correct. I just used another register rather than declaring a variable. I was scared to because I thought it would fuck up last:.

Here is what I got, this is literally our first homework so dont kill me if it looks noobish...because it is

;; Thi s assembly program, hw1_1 evaluates the expression
;; (2n^2 - 1)^2 + n^3, with n being put into ECX and the output being moved
;; to EAX.
;; 
;; 

;; Register usage:
;;      EAX     Output
;;      ECX     Original value of n
;;      EDX     Placeholder for n
;;      EBX     Placeholder for n       

[SECTION .text]

        global  main

main:
        push    EBP
        mov     EBP, ESP
        push    EBX
        push    ESI
        push    EDI


first:
        add     ECX, 0
        add     EAX, 0
        add     EBX, 0
        add     EDX, 0
top:


        mov     EBX, ECX
        imul    EBX, EBX
        imul    EBX, 2
        sub     EBX, 1
        imul    EBX, EBX
        mov     EDX, ECX
        imul    EDX, EDX
        imul    EDX, ECX
        add     EBX, EDX
        mov     EAX, EBX
last:
        pop     EDI
        pop     ESI
        pop     EBX
        mov     ESP, EBP
        pop     EBP
        ret

Edited 5 Years Ago by peter_budo: Snipped name

Hi,
What is the reason to add zero to the registers. This has no effect. Why?.
The rest of the code is very good.

Best regards

In case they weren't set to 0 for some reason my teacher did it on his programs so I just adopted it... I dont really know, its kinda like a starting point for me, makes me feel all warm and fuzzy

Edited 5 Years Ago by rcossy1023: n/a

If you have, for instance, in eax the value 2 and you add 0 to eax, eax will contain 2. Add zero has no effect.
If you want a register with a zero value, you can do it with 'mov eax, 0' or, as prefered by some programers: 'xor eax, eax'.

This question has already been answered. Start a new discussion instead.