Jump to content

Hello everyone,

I am new to assembly and I need to do this exercise:

 

Implement an Assembly function to perform the following operation:

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.

 

I don't know how to make the operantions between the different types

Any help is appreciated

Link to comment
https://linustechtips.com/topic/678372-assembly-problem/
Share on other sites

Link to post
Share on other sites

46 minutes ago, Nicholatian said:

You probably want to give us the architecture you’re using; I’d gander given the problem you’re probably using x86. Sorry I can’t be of more help with that, since I write ARM assembly.

Linux 32 bit (sry if not specific enough)

Link to comment
https://linustechtips.com/topic/678372-assembly-problem/#findComment-8729027
Share on other sites

Link to post
Share on other sites

1 hour ago, gabrielcarvfer said:

You need to specify Architecture (arm, mips, i368/x86, amd64/x86_64, ... ) and Assembly language ( nasm, masm, gas, intel, ... ).

 

In NASM, you can do that with the following:
 


section .data
a db 1
b dw 2
c dq 3
d dq 4

section .text
global CMAIN
CMAIN:

    ;being a and b pointers to those variables
    mov ah, 0   ; load higher 8 bits with 0
    mov al, [a] ; load lower 8 bits with content from a address
    mov bx, [b] ; load 16 bits with content from b address
    add bx, ax  ; sum a and b contents to bx, meaning its a 16 bit 
    
    ;being c and d pointers to those variables
    mov ecx, [c]
    mov edx, [d]
    sub edx, ecx ; subtracts c content from d content and store in edx 
    
    ; now clear higher 16 bits from ebx
    and ebx, 0000FFFFh
    
    ; sum ebx and edx
    add ebx, edx 
    
    ; move results to rax, that is going to be returned to caller
    mov rax, 0
    mov eax, ebx
  
    ;return to caller
    ret

That's just an example, step by step, but you can do the same thing with less instructions.

Thank you!

Link to comment
https://linustechtips.com/topic/678372-assembly-problem/#findComment-8730019
Share on other sites

Link to post
Share on other sites

14 hours ago, gabrielcarvfer said:

You need to specify Architecture (arm, mips, i368/x86, amd64/x86_64, ... ) and Assembly language ( nasm, masm, gas, intel, ... ).

 

In NASM, you can do that with the following:
 


section .data
a db 1
b dw 2
c dq 3
d dq 4

section .text
global CMAIN
CMAIN:

    ;being a and b pointers to those variables
    mov ah, 0   ; load higher 8 bits with 0
    mov al, [a] ; load lower 8 bits with content from a address
    mov bx, [b] ; load 16 bits with content from b address
    add bx, ax  ; sum a and b contents to bx, meaning its a 16 bit 
    
    ;being c and d pointers to those variables
    mov ecx, [c]
    mov edx, [d]
    sub edx, ecx ; subtracts c content from d content and store in edx 
    
    ; now clear higher 16 bits from ebx
    and ebx, 0000FFFFh
    
    ; sum ebx and edx
    add ebx, edx 
    
    ; move results to rax, that is going to be returned to caller
    mov rax, 0
    mov eax, ebx
  
    ;return to caller
    ret

That's just an example, step by step, but you can do the same thing with less instructions.

Are there any differences in the registers for Ia32?

I've tried several things but can't seem to make the rax and "0000FFFh" part work

I am really new to this and still figuring a lot of things out

 

Thank you for the help

Link to comment
https://linustechtips.com/topic/678372-assembly-problem/#findComment-8732812
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

×