flat assembler
Message board for the users of flat assembler.

 Index > Main > About cbw,cwde,cwd,cdq
Author
Roman

Joined: 21 Apr 2012
Posts: 1709
Roman 25 Apr 2023, 10:20
Why need cbw,cwde,cwd,cdq ?
cbw — sign-extend byte in al to word in ax. Why do this ?

Show example task where is needed cbw,cwde,cwd,cdq.
25 Apr 2023, 10:20
Roman

Joined: 21 Apr 2012
Posts: 1709
Roman 25 Apr 2023, 10:27
O !
25 Apr 2023, 10:27
Roman

Joined: 21 Apr 2012
Posts: 1709
Roman 25 Apr 2023, 10:27
I found. For negative numbers.

Code:
```xor eax,eax
mov al,-5 ;-5=255-5=251
cbw       ;in ax=-5 but 16bits 65535-5
```
25 Apr 2023, 10:27
macomics

Joined: 26 Jan 2021
Posts: 918
Location: Russia
macomics 25 Apr 2023, 11:17
Forgot cqo. rax => rdx:rax
25 Apr 2023, 11:17
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 20175
Location: In your JS exploiting you and your system
revolution 25 Apr 2023, 11:21
In twos complement:
0xFB == -5
0xFFFB == -5

In unsigned:
0xFB == 251
0xFFFB == 65531

What happens if you add 65531 + 65531 with 16-bit registers?
0xFFFB + 0xFFFB == 0xFFF6 --> -5 + -5 == -10

What happens when you add 65531 + 5 with 16-bit registers?
0xFFFB + 0x0005 == 0x0000 --> -5 + 5 == 0
25 Apr 2023, 11:21
macomics

Joined: 26 Jan 2021
Posts: 918
Location: Russia
macomics 25 Apr 2023, 11:27
In order not to ask such questions, it is enough to read about the additional format of numbers.

https://en.wikipedia.org/wiki/Two%27s_complement#Sign_extension
25 Apr 2023, 11:27
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 20175
Location: In your JS exploiting you and your system
revolution 25 Apr 2023, 11:33
I think a lot of confusion happens if someone implicitly extends values with zeros.

The following might not be true:
0xFB == 0x00FB
We need to justify whether extending to the left with zeros is correct, or whether extending with the top-most bit is correct. And it depends upon the context.
25 Apr 2023, 11:33
Roman

Joined: 21 Apr 2012
Posts: 1709
Roman 25 Apr 2023, 11:35
macomics wrote:
Forgot cqo. rax => rdx:rax

Why out to rdx:rax ?
25 Apr 2023, 11:35
Roman

Joined: 21 Apr 2012
Posts: 1709
Roman 25 Apr 2023, 11:38
Oh !
Code:
```mov     al,-5 ;al=FB
movsx ax, al  ;the same work as cbw ! I checked in IDA Pro ax=FFFB

;more handful then cbw,cwde,cwd
movsx   edx,cl
movsx   dx,cl
movsx   edx,cx
```

Crazy Intel

Last edited by Roman on 25 Apr 2023, 12:09; edited 7 times in total
25 Apr 2023, 11:38
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 20175
Location: In your JS exploiting you and your system
revolution 25 Apr 2023, 11:38
Roman wrote:
macomics wrote:
Forgot cqo. rax => rdx:rax

Why out to rdx:rax ?
For div
25 Apr 2023, 11:38
macomics

Joined: 26 Jan 2021
Posts: 918
Location: Russia
macomics 25 Apr 2023, 12:13
revolution wrote:
Roman wrote:
macomics wrote:
Forgot cqo. rax => rdx:rax

Why out to rdx:rax ?
For div
For idiv
25 Apr 2023, 12:13
DimonSoft

Joined: 03 Mar 2010
Posts: 1228
Location: Belarus
DimonSoft 27 Apr 2023, 14:30
Roman wrote:
Oh !
Code:
```mov     al,-5 ;al=FB
movsx ax, al  ;the same work as cbw ! I checked in IDA Pro ax=FFFB

;more handful then cbw,cwde,cwd
movsx   edx,cl
movsx   dx,cl
movsx   edx,cx
```

Crazy Intel

It’s just important to note that movsx and movzx didn’t exist before 80386.
27 Apr 2023, 14:30
Ali.Z

Joined: 08 Jan 2018
Posts: 676
Ali.Z 16 May 2023, 18:23
you can play with the snippet here, trying removing cdq and see what happen.

https://board.flatassembler.net/topic.php?t=22616

_________________
Asm For Wise Humans
16 May 2023, 18:23
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

 Jump to: Select a forum Official----------------AssemblyPeripheria General----------------MainTutorials and ExamplesDOSWindowsLinuxUnixMenuetOS Specific----------------MacroinstructionsOS ConstructionIDE DevelopmentProjects and IdeasNon-x86 architecturesHigh Level LanguagesProgramming Language DesignCompiler Internals Other----------------FeedbackHeapTest Area

Forum Rules:
 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot vote in polls in this forumYou cannot attach files in this forumYou can download files in this forum