Jump to content

Properly use operations on 32 bit registers

ShatteredPsycho

Hello everyone,

 

I have this exercise:

A + B – C + D.
A is a 8-bit variable, B is a 16-bit variable, while C and D are both 32-bit variables. The function
should return a 64-bit value that must be printed in C.

Which I am certanly doing wrong, mostly because I am not sure yet what operations I should use to do it right.

I did this just to have something printed out:

movl op1, %ebx	#move op1 to a register
movl op2, %eax	#move op1 to a register
addl %eax, %ebx #add op1 and op2
movl op3, %eax  #move op3 to register
addl %eax, %ebx #subtract op3 to result of op1+op2
movl op4, %eax #move op4 to register
addl %eax, %ebx #add op4 to previous result

How do I go about this properly?

Link to comment
Share on other sites

Link to post
Share on other sites

What kind of hardware is this for?

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

30 minutes ago, Sauron said:

What kind of hardware is this for?

Just a school exercise, running a VM with Linux 32bit

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, geo3 said:

Maybe I'm missing something but how do you have a 64bit integer on a 32bit OS?

Mind blown.

Link to comment
Share on other sites

Link to post
Share on other sites

On 21/10/2017 at 1:50 AM, gabrielcarvfer said:

He didn't used any 64bit instruction/registers, he's using only sums and subtractions and he can implement 64bit operations/types using those 32bit registers, so that shouldn't be a problem.

 

 

First of all, which assembly are you using? Intel, Gas, Nasm, ...?

If using Nasm, you can only mov stuff from registers with same size or data, so you can't move 8bit stuff to 32bit register. If you try to do that, you will end up moving 32bits, including the 8bit data you wanted, but 3 other bytes of whatever is next to your data.
 

  Reveal hidden contents


global asm_func

asm_func:
;before doing anything, save registers you plan to use
push eax ; save eax into stack
push ebx ; save ebx into stack
push ecx ; save ecx into stack
push edx ; save edx into stack

sub ah, ah   ; set AH (higher 8 bits of AX) to 0
sub ebx, ebx ; set EBX (32 bit) to 0

mov al, [A] ; set AL (lower 8 bits of AX) to A content
mov bx, [B] ; set BX (16 bit) to B content

add bx, ax   ; BX = BX + AX

mov ecx, [C]; set ECX (32 bit) to C content
mov edx, [D]; set EDX (32 bit) to D content

sub edx, ecx ; EDX = EDX - ECX
add edx, ebx ; EDX = AL + BX - ECX + EDX

mov [OUTPUT], edx
sub eax, eax ; set eax = 0

;In case of negative number
bt eax, 31 ; check the 32th bit, if 1, negative so you need to extend signal
jge .number_is_positive ; jump to label if the number is positive

sub eax, 1 ; set eax = -1 = 0xFFFF FFFF FFFF FFFF
mov [OUTPUT+4], eax ; set output+4bytes (the higher 32bit of 64bit soft number) as 0xFFF...)
jmp .end

.number_is_positive:
mov [OUTPUT+4], eax ; set output+4bytes (the higher 32bit of 64bit soft number as 0x000...)

.end:

;before returning, undo the register saving with the same order you did in the start
push edx ; load edx from stack
push ecx ; load ecx from stack
push ebx ; load ebx from stack
push eax ; load eax from stack

;return to callee function
ret

 

 

I am not really sure, does IA32 GNU say somehting?

E obrigado pela ajuda outra vez ;)

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×